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 ?

Who is Participating?
AlexFMConnect With a Mentor Commented:
Use GetLastInputInfo API.

The GetLastInputInfo function retrieves the time of the last input event.

BOOL GetLastInputInfo(
  PLASTINPUTINFO plii   // last input event

[out] Pointer to a LASTINPUTINFO structure that receives the time of the last input event.

typedef struct tagLASTINPUTINFO {
  UINT cbSize;
  DWORD dwTime;

See also: GetTickCount API.
Take a look at this code:

using System;
using System.Runtime.InteropServices;

namespace Test1
    class Class1
        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>
        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);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.