2

I have a desktop application in which I would like to know two things:

  1. Is the user currently on the PC (more specifically, is he giving any input to the PC), so I can change his state to "away" if needed; and
  2. Is the screensaver running right now, so I can perform more CPU intensive work during that time.

I'm using C#/.NET. How would you suggest to tackle these two tasks?

NOTE: WIN32 invocation will be just as good, as well as any unmanaged code solution.

4

4 回答 4

5

这是检测屏幕保护程序是否正在运行的代码。有关更多详细信息,请参阅

const int SPI_GETSCREENSAVERRUNNING = 114;

[DllImport( "user32.dll", CharSet = CharSet.Auto )]
private static extern bool SystemParametersInfo( 
   int uAction, int uParam, ref bool lpvParam, 
   int flags );


// Returns TRUE if the screen saver is actually running
public static bool GetScreenSaverRunning( )
{
   bool isRunning = false;

   SystemParametersInfo( SPI_GETSCREENSAVERRUNNING, 0, 
      ref isRunning, 0 );
   return isRunning;
}
于 2010-12-23T06:23:03.173 回答
5

http://dataerror.blogspot.com/2005/02/detect-windows-idle-time.html

^ 检测 Windows 空闲时间。:)

此功能的启用程序是GetLastInputInfo() Win32 API和 LASTINPUTINFO Win32 结构。

于 2010-12-23T06:17:58.867 回答
0

您可以使用全局键盘/鼠标挂钩,并在收到来自任一事件的事件时将“计数器”重置为 0。当您的计数器达到您正在寻找的空闲时间时,执行您的后台操作。

这里有一些代码可以让您轻松地在 .NET 中进行挂钩:http: //www.codeproject.com/KB/cs/globalhook.aspx

于 2010-12-23T06:21:57.867 回答
0

而不是弄清楚何时运行更密集的工作......考虑尽可能早地进行“密集工作”,但线程优先级较低。

我认为您的问题在纯 C# 中没有答案,除非您轮询鼠标位置并观察移动......或类似的东西。

于 2010-12-23T05:59:36.043 回答