检查用户是否处于非活动状态 [英] Check whether user is inactive

查看:202
本文介绍了检查用户是否处于非活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查用户是无效的? 我有这样的:

How do I check user is inactive? I have this:

class UserActivity : IMessageFilter
{
    private double afk_time = 0.1;//minutes
    private DateTime last_activity = DateTime.Now;
    public static bool inactive = false;

    private int WM_LBUTTONDOWN = 0x0201;
    private int WM_MBUTTONDOWN = 0x0207;
    private int WM_RBUTTONDOWN = 0x0204;
    private int WM_MOUSEWHEEL = 0x020A;
    private int WM_MOUSEMOVE = 0x0200;
    private int WM_KEYDOWN = 0x0100;

    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == WM_LBUTTONDOWN || m.Msg == WM_MBUTTONDOWN || m.Msg == WM_RBUTTONDOWN || m.Msg == WM_MOUSEWHEEL || m.Msg == WM_MOUSEMOVE || m.Msg == WM_KEYDOWN)
        {
            this.last_activity = DateTime.Now;
            inactive = false;
        }

        if (DateTime.Now.AddMinutes(-afk_time) > last_activity)
            inactive = true;

        return false;
    }
}

不过,我必须在Program.cs中运行它

But I must run it in Program.cs

Application.AddMessageFilter(new UserActivity());

我如何能做到这一点,我可以用我的自我运行的用户不活动的检查。我查了一些复选框,这将启动检查。

How can I do that I can run the checking of user inactivity by my self. I'll check some checkbox and it will start checking.

和我想查询全球用户活动 - 不仅在应用程序中的所有系统

And I want check global user activity - in all system not only in app.

我不希望使用CPU没有必要的。或者,我应该用另一种解决方案?

I don't want use of cpu unnecessary. Or should I use another solution?

推荐答案

我发现这和它的作品完美! 因此,如果另一个有问题,在这里是解决方案:

I found this and it works perfect ! So if another one have problem with it here is solution:

    [StructLayout(LayoutKind.Sequential)]
    public struct LASTINPUTINFO
    {
        public uint cbSize;
        public uint dwTime;
    }

    [DllImport("user32.dll")]
    static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);


    public static TimeSpan? GetInactiveTime()
    {
        LASTINPUTINFO info = new LASTINPUTINFO();
        info.cbSize = (uint)Marshal.SizeOf(info);
        if (GetLastInputInfo(ref info))
            return TimeSpan.FromMilliseconds(Environment.TickCount - info.dwTime);
        else
            return null;
    }

这篇关于检查用户是否处于非活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆