Check whether user is inactive

 ̄綄美尐妖づ 提交于 2019-12-23 03:01:50

问题


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;
    }
}

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.

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


回答1:


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;
    }



回答2:


I have tried to build a class base on previous answer, I have test it and it work properly. It is async but i have not use async key. If someone finds some problem or amelioration so welcome.

public class User
{
    [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;
    }

    private TimeSpan? LastTime;
    private DispatcherTimer timer = new DispatcherTimer();
    public void RequestUserActivity()
    {
        LastTime = GetInactiveTime();
        timer.Interval = TimeSpan.FromMilliseconds(500);
        timer.Tick += timer_Tick;
        timer.Start();
    }

    void timer_Tick(object sender, EventArgs e)
    {
        TimeSpan? tmpTime = GetInactiveTime();
        if (LastTime.HasValue && tmpTime.HasValue && tmpTime < LastTime)
        {
            timer.Stop();
            UserAtivited(this, new EventArgs());
        }
        else if (!LastTime.HasValue || !tmpTime.HasValue)
        {
            timer.Stop();
            throw new Exception();
        }
    }

    public event EventHandler UserAtivited;
}

Now In your window create a new instance of User, register to UserActivated event and laugth RequestUserActivity(). I expert it will be helpfull.


Please, do not have regards on my poor english. I'm french speaker.



来源:https://stackoverflow.com/questions/10776027/check-whether-user-is-inactive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!