Link to home
Start Free TrialLog in
Avatar of renjith
renjith

asked on

Tracking system idle time

I am creating a component that has to do windows screen lock based on the system idle time.
Say if the system is idle for 15 mins it should lock the screen. How can I track the system idle time ?
Is there any readymade utility for this ?


Regards
Renjith
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexFM
AlexFM

Take a look at this code:

using System;
using System.Runtime.InteropServices;

namespace Test1
{
    class Class1
    {
        [StructLayout(LayoutKind.Sequential)]
        struct LASTINPUTINFO
        {
            public int cbSize;
            public int dwTime;
        }

        [DllImport ("User32.dll")]
        static extern void GetLastInputInfo(ref LASTINPUTINFO info);

        [DllImport ("Kernel32.dll")]
        public static extern int GetTickCount();

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            LASTINPUTINFO info = new LASTINPUTINFO();
            info.cbSize = Marshal.SizeOf(info);

            GetLastInputInfo(ref info);
            int n = GetTickCount();

            int nIdle = n - info.dwTime;

            Console.WriteLine("Idele time (ms): {0}", nIdle);
        }
    }
}