2011-05-04 71 views
2

可能重复:
Check if an application is idle for a time period and lock it如何检测我的应用程序在c#中空闲?

假设我有一个C#开发的Windows产品。现在需要的是,如果应用程序正在运行并且处于空闲状态,并且用户尝试再次与该应用程序进行交互,则会出现登录屏幕。如何检测我的应用程序在运行时是否处于空闲状态?请指导我完成这项工作。

+3

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

+0

我发现几个链接,请检查它们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

回答

6
  1. 将计时器控件添加到您的 应用程序中。
  2. Subscribe to mouseover and keydown events - when they fire,reset the timer。
  3. 当定时器发生火灾时(例如,鼠标 未移动且按键的数量未按下0x35时,按下 ),锁定 登录的屏幕/提示。
+1

订阅鼠标悬停可能会非常频繁地触发定时器。可能Keydown或mousedown事件可能是很好的订阅。 – CharithJ 2011-05-04 11:55:37

+0

是的 - 你需要看看什么最适合你的需求。 (虽然重置计时器并不昂贵,所以不要担心) – Nathan 2011-05-04 11:58:11

0

捕获Form类对象的离开事件以知道它已失去焦点。

1

您可以使用Control.LostFocus记录时用户导航离开然后使用Control.GotFocus检查多少时间已经过去了,以确定他们是否需要登录。

1

何去何从我简单的解决方案:

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”来检查每个间隔。