zoukankan      html  css  js  c++  java
  • (C#)IdleMonitor——实现监测用户是否离开应用程序

    写了一个C#的小程序,用来监测用户是否离开应用程序,就像MSN,QQ的离开功能一样。代码在github上。简单接介绍一下:

    1. IdleMonitor abstract class

        public abstract class IdleMonitor
        {
            ...
            public event EventHandler TimeoutEventHandler;
            protected IdleMonitor(TimeSpan timeout){...}
            public abstract void Start();
            public abstract void Stop();
            ...
        }

    在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。

    image

    打开IdleMonitor。

    image

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

    image

    在WinForm中的例子类似。

  • 相关阅读:
    获取系统版本
    一句代码删除所有子视图
    MAJOR-MINOR-MKDEV
    AF_UNIX和AF_INET域的socket在epoll中的差异
    python-print
    python-class(5)
    python-class(4)
    python-class(3)
    python-class(2)
    python-class(1)
  • 原文地址:https://www.cnblogs.com/fresky/p/2782095.html
Copyright © 2011-2022 走看看