2011-04-01 46 views
5

我使用了代码here来计算通过窗口服务的系统空闲时间。但在Vista中,我总是将LastInputInfo.dwTime设为零,因此我无法计算系统空闲时间。我已经在下面给出了代码示例。使用C#中的窗口服务无法在Windows 7中计算系统空闲值

// 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; 
} 

当我在Windows应用程序测试相同的代码我得到正确的dwTime。我需要在我的服务中有正确的dwTime才能计算系统的正确空闲时间。

请一些例子

回答

2

我不认为你将能够使用GetLastInputInfo在Windows服务在Windows 7这不是关于代码的帮助,这是关于服务于Vista和更高版本硬化。

阅读:Services isolation in Session 0 of Windows Vista and Longhorn Server

+0

有什么办法绕过用户会话 – deepu 2011-04-01 13:24:55

+0

嗯,这使..服务运行,您可以最终创建标准的Windows应用程序(Windows启动时,将启动)将发送空闲时间您的服务通过一些IPC(进程间通信)机制。 – HABJAN 2011-04-01 13:27:03

+0

或maby你可以帮助你自己处理CPU空闲时间(如果你需要的话)。看看这篇文章:http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c39b416c-e959-41e2-a57e-c02f7b22cc1d – HABJAN 2011-04-01 13:29:57

相关问题