public static int GetIdleTime() { return (Environment.TickCount & Int32.MaxValue)- (int)GetLastInputTime(); } /// /// Get the last input time from the input devices. /// Exception: /// If it cannot get the last input information then it throws an exception with /// the appropriate message. /// /// Last input time in milliseconds. public static uint GetLastInputTime() { LastInputInfo lastInPut = new LastInputInfo(); lastInPut.BlockSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut); if (!GetLastInputInfo(ref lastInPut)) { throw new Exception(GetLastError().ToString()); } return lastInPut.Time; }