如何获取系统空闲时间(以毫秒为单位)
我已经使用它很长时间了,但现在我发现了一个错误,即 48 天后它没有给我正确的空闲时间。
这是由于整数32位造成的。我可以设法通过 Environment.TickCount 不够 获得很长的滴答计数器
但是,我的需要是获取空闲时间和 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;
}
I have been using this for long time but now I have found the bug that after 48 days it dos not give me the right idle time.
It is due to the integer32 bits. I could manage to get the tick counter in long through Environment.TickCount is not enough
However, my need is to get the idletime and GetLastInputTime() [http://www.codeproject.com/KB/system/rtwidledll.aspx?display=PrintAll] is not returning me long.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论