可能重复:
Check if an application is idle for a time period and lock it如何检测我的应用程序在c#中空闲?
假设我有一个C#开发的Windows产品。现在需要的是,如果应用程序正在运行并且处于空闲状态,并且用户尝试再次与该应用程序进行交互,则会出现登录屏幕。如何检测我的应用程序在运行时是否处于空闲状态?请指导我完成这项工作。
可能重复:
Check if an application is idle for a time period and lock it如何检测我的应用程序在c#中空闲?
假设我有一个C#开发的Windows产品。现在需要的是,如果应用程序正在运行并且处于空闲状态,并且用户尝试再次与该应用程序进行交互,则会出现登录屏幕。如何检测我的应用程序在运行时是否处于空闲状态?请指导我完成这项工作。
捕获Form类对象的离开事件以知道它已失去焦点。
您可以使用Control.LostFocus记录时用户导航离开然后使用Control.GotFocus检查多少时间已经过去了,以确定他们是否需要登录。
何去何从我简单的解决方案:
Point cursorPoint;
int minutesIdle=0;
private bool isIdle(int minutes)
{
return minutesIdle >= minutes;
}
private void idleTimer_Tick(object sender, EventArgs e)
{
if (Cursor.Position != cursorPoint)
{
// The mouse moved since last check
minutesIdle = 0;
}
else
{
// Mouse still stoped
minutesIdle++;
}
// Save current position
cursorPoint = Cursor.Position;
}
您可以设置在60000区间运行的定时器。通过这种方式,您只需知道用户不会移动鼠标多少分钟。您也可以在Tick事件本身上调用“isIdle”来检查每个间隔。
Dupicated http://stackoverflow.com/questions/1541981/check-if-an-application-is-idle-for-a-time-period-and-lock-it – 2011-05-04 11:37:31
我发现几个链接,请检查它们http://msdn.microsoft.com/en-us/library/system.windows.forms.application.idle.aspx http://blog.perfectapi.com/2008/05/detecting-idle-state-in-winforms -apps/ – SharpUrBrain 2011-05-04 12:23:29