开发者

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

I have a desktop application in which I 开发者_Python百科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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜