我已经使用了很长时间但是现在我发现了48天之后它没有给我正确的空闲时间的错误。
这是由于整数32位。我可以设法通过Environment.TickCount is not enough
来获取刻度计数器但是,我需要获得空闲时间和GetLastInputTime()[http://www.codeproject.com/KB/system/rtwidledll.aspx?display=PrintAll]并没有让我久久。
public static int GetIdleTime()
{
return (Environment.TickCount & Int32.MaxValue)- (int)GetLastInputTime();
}
/// <summary>
/// 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.
/// </summary>
/// <returns>Last input time in milliseconds.</returns>
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;
}