1

I am using InactivityDetector code to detect inactivity from user. But now I'm facing a problem where I need to reset the IdleTime without making any user interaction.

I already tryed in another ways to reproduce this result, like simulating mouse events or key press, but none of them are working because they are not physical interaction from the user.

And yes I have already searched for so many solutions in StackOverflow but no solution found for my problem. Anyone can help in this?

My InactivityDetector class:

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


        public static IdleTimeInfo GetIdleTimeInfo()
        {
            int systemUptime = Environment.TickCount,
                lastInputTicks = 0,
                idleTicks = 0;

            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            foreach (Window openWin in Application.Current.Windows)
            {
                //ignore activity from pc when shell view is minimized (for counting unactivity only from application)
                if (openWin.DataContext != null && openWin.DataContext.GetType() == typeof(ShellViewModel) 
                    && openWin.WindowState != WindowState.Minimized)
                {
                    if (GetLastInputInfo(ref lastInputInfo))
                    {
                        lastInputTicks = (int)lastInputInfo.dwTime;

                        idleTicks = systemUptime - lastInputTicks;
                    }
                }
            }

            /*if (GetLastInputInfo(ref lastInputInfo))
            {
                lastInputTicks = (int)lastInputInfo.dwTime;

                idleTicks = systemUptime - lastInputTicks;
            }*/

            return new IdleTimeInfo
            {
                LastInputTime = DateTime.Now.AddMilliseconds(-1 * idleTicks),
                IdleTime = new TimeSpan(0, 0, 0, 0, idleTicks),
                SystemUptimeMilliseconds = systemUptime,
            };
        }
    }

    public class IdleTimeInfo
    {
        public DateTime LastInputTime { get; internal set; }

        public TimeSpan IdleTime { get; internal set; }

        public int SystemUptimeMilliseconds { get; internal set; }
    }

    internal struct LASTINPUTINFO
    {
        public uint cbSize;
        public uint dwTime;
    }
Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26

0 Answers0