(C#)IdleMonitor 实现监测用户是否离开应用程序
Contents
写了一个C#的小程序,用来监测用户是否离开应用程序,就像MSN,QQ的离开功能一样。代码在github上。简单接介绍一下:
1. IdleMonitor abstract class
1 | public abstract class IdleMonitor |
在WinForm和WPF的应用中,只需要通过IdleMonitorFactory得到一个IdleMonitor,然后调用Start()来开始监测,调用Stop()来结束监测。应用程序可以加event handler到TimeoutEventHandler上,这样当时间到了的时候就能被调到。
2. IdleMonitor 的实现。
实现了3中IdleMonitor。
(1)GetLastInputInfoIdleMonitor。
使用了GetLastInputInfo API。WinForm和WPF都能用。
(2)MessageFilterIdleMonitor。
使用了MessageFilter,过滤键盘和鼠标消息。所有的消息列表在这里。WinForm和WPF都能用。
(3)ComponentDispatcherIdleMonitor。
用到了Operationposted and ThreadIdle 事件。只能在WPF中使用。
3. WinForm和WPF的使用例子。
combobox中是可选的3种IdleMonitor。

打开IdleMonitor。

时间到的时候会调用wpf中写好的event handler,抛如下对话框。

在WinForm中的例子类似。