Detecting that the user is away from the PC with .NET

梦想与她 提交于 2019-11-29 12:04:13
Phill

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

^ Detect Windows Idle Time. :)

The enabler for this feature is the GetLastInputInfo() Win32 API and the LASTINPUTINFO Win32 structure.

Here is the code to detect if a screen saver is running. See this for more details

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

Rather than figuring out when to run more intensive work... Consider doing your "intensive work" as early as you can, but at a lower thread priority.

I don't think your questions have an answer in pure C#, unless you poll the mouse position and observe movements... Or something like that.

You could use a global keyboard/mouse hook and just reset your "counter" to 0 when you receive an event from either. When your counter reaches the idle time that you're looking for, perform your background actions.

There is some code here that allows you to easily do the hooking in .NET: http://www.codeproject.com/KB/cs/globalhook.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!