Check whether user is inactive

4.4k views Asked by At

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?

2

There are 2 answers

1
sczdavos On

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;
    }
0
Max Winner On

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.