比如一段时间不动就自动退出一样!解决了就给分,决不失言!
解决方案 »
- 菜鸟实现定时自动发信息
- 关于WCF路由中的 backupList功能的问题----------------------??
- 求及时通讯开发方面的思路及方法,有源码和文档更好;
- 下载网页上的图片
- 医院管理系统B/S或C/S
- win7下vs2003 无法 加载web项目
- 请问如何将form1中的textbox1.text内容,复值给form2中的listview?
- DataSet中的列设置了Expression属性,可是一定要在EndEdit()后才计算,能否实时计算?
- ==========求一个正则,盼高手指点==========
- 女生学C# 不容易 这个算法我真的高了很多天没搞懂 球大神小哥哥们帮我
- 数组问题!不太好表述
- c#.net 网路流传输图片问题
今天有客户抱怨我们的程序不安全,说人走了以后不能自动退出。没有办法,谁能帮我想想设置一个timeout,如果用户一定时间没有动键盘鼠标,就退出程序。
http://www.cnblogs.com/michaelxu/archive/2006/09/22/511557.aspx
http://support.microsoft.com/default.aspx?scid=kb;en-us;318804
{
base.WndProc(ref msg);
tick=Environment.TickCount;
}
Timer事件里写判断代码,记的给我分啊!!
if(Environment.TickCount-tick>5000) this.Close();
然后在Application.Idle事件里作考虑.
http://www.codeproject.com/csharp/ApplicationIdle.asp
===========================
可以设置session值,比如登陆时保存登陆的用户ID,在一些频繁按钮或操作上加上session值是否为null的判断,为null就退出。我现在的项目就这样的,一般session值的超时为20分钟
=============================
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace Mouse_Move
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private int tick=0; protected override void WndProc(ref Message msg)
{
base.WndProc(ref msg);
tick=Environment.TickCount;
} private void timer1_Tick(object sender, EventArgs e)
{
if(Environment.TickCount-tick>2000) this.Close();
}
}
}
先设置一个变量来存放多久没有键盘和鼠标输入就退出程序,如 int timeExit=5
再设置一个变量来存放当前已有多久没有键盘和鼠标输入,如 int timeNow=0(初始值为0)
然后拖一个timer控件,将其Interval属性设为1000。双击Timer控件,在timer1_Tick对应的事件中输入:
timeNow=timeNow+1;
if(timeNow>=timeExit) this.close();
然后就是当有键盘或鼠标动作时就将tiemNow清零,使其重新计数。因为有鼠标动作时,鼠标肯定会移动,因此在程序窗口(默认为Form1)的Form1_MouseMove事件中输入:
timeNow=0;
this.timer1.Enable=false;
this.timer1.Enable=true;
之所以要将控件timer1先禁用再启用,是为让其重新开始计算时间。
同样,有键盘动作时都会有键按下,因此将程序窗口(默认为Form1)的Form1_KeyPress事件设置为Form1_MouseMove即可。
注意:这里所说的没有键盘或鼠标动作是指在程序具有焦点时的情况,若程序不具有焦点,就算有键盘或鼠标动作也会退出,不过这个可以通过在其退出之前使其具有焦点,并寻问用户是否退出。
总共用了两个timer控件,第二个控制提示对话框。第一个timer1的启动时的Enable属性为true,而第二个的为false。
private int timeExit = 5;
private int timeExit2 = 3;
private int timeNow = 0;
private int timeNow2 = 0;
private void timer1_Tick(object sender, EventArgs e)
{
timeNow = timeNow + 1;
if (timeNow >= timeExit)
{
this.timer1.Enabled = false;
this.timer2.Enabled = true;
DialogResult exitResult = MessageBox.Show("您真的要退出吗?", "确认", MessageBoxButtons.YesNo);
if (exitResult == DialogResult.Yes)
this.Close();
else
{
this.timeNow = 0;
timer1.Enabled = true;
this.timer2.Enabled = false;
this.timeNow2 = 0;
}
}
} private void Form1_MouseMove(object sender, MouseEventArgs e)
{
timeNow = 0;
this.timer1.Enabled = false;
this.timer1.Enabled = true;
} private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
timeNow = 0;
this.timer1.Enabled = false;
this.timer1.Enabled = true;
} private void timer2_Tick(object sender, EventArgs e)
{
this.timeNow2 = this.timeNow2 + 1;
if (timeNow2 >= timeExit2)
this.Close();
}
这是我在CSDN上第一次回答问题,希望能被采纳!
设立一个Timer,和变量n
然后在form的MouseMov和KeyPress事件被触发则修改n为0。
Timer设置成每隔一段时间则变量n++,然后当n到达一定的数值后则关闭程序。这就是我的思路,不知道是否可行。
尤其是,可能产生同时有多个Timer都在计算,都有各自的变量,Form1打开Form2,Form1的Timer没有停止计算,但是,此时,是Form2有鼠标和键盘事件,那么,Form1不是肯定会在5分钟关闭程序吗?