使用C#中的窗口服务无法在Windows 7中计算系统空闲值

dee*_*epu 5 c# windows-services windows-7

我已经使用这里提到的代码来计算通过窗口服务的系统空闲时间.但是在Windows 7和Vista中,我总是得到LastInputInfo.dwTime0,因为我无法计算系统空闲时间.我已经给出了下面的代码示例.

 // If we have a value from the function
if (GetLastInputInfo(ref LastInputInfo))
{
    // Get the number of ticks at the point when the last activity was seen
    LastInputTicks = (int)LastInputInfo.dwTime;
    // Number of idle ticks = system uptime ticks - number of ticks at last input
    IdleTicks = systemUptime - LastInputTicks;
}
Run Code Online (Sandbox Code Playgroud)

当我在Windows应用程序中测试相同的代码时,我得到了正确的答案dwTime.我需要dwTime在我的服务中正确计算系统的正确空闲时间.

请帮助一些例子

HAB*_*JAN 2

我认为您无法在 Windows 7 上的 Windows 服务中使用 GetLastInputInfo。这与代码无关,而是与 Vista 及更高版本中的服务强化有关。

阅读本文:Windows Vista 和 Longhorn Server 的会话 0 中的服务隔离