使用.NET检测用户是否远离PC

Eld*_*Mor 2 .net c# user-input screensaver

我有一个桌面应用程序,我想知道两件事:

  1. 用户当前是否在PC上(更具体地说,他是否向PC提供任何输入),因此如果需要,我可以将其状态更改为"离开"; 和
  2. 屏幕保护程序是否正在运行,因此我可以在此期间执行更多CPU密集型工作.

我正在使用C#/ .NET.您如何建议解决这两项任务?

注意:WIN32调用和任何非托管代码解决方案一样好.

Phi*_*ill 5

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

^检测Windows空闲时间.:)

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

  • @Charles - 我知道,但我写了这个早在2010年,我想我当时刚刚加盟SO也没有人向我指出"链接不是答案"的信息.:) (2认同)

Har*_*wok 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;
}
Run Code Online (Sandbox Code Playgroud)