开发者

.NET detect user activity system-wide

I have a Windows Service that runs a CPU-intensive job. I have it installed on several computers. The problem is, when it runs the machine is wicked slow. So I'd like to have the service only run during periods of inactivity....just like a screensaver.

I know I can use low-level keyboard/mouse hooks like in http://www.codeproject.com/KB/system/globalsystemh开发者_JAVA技巧ook.aspx. I'm wondering though if Windows has a more sophisticated API that is specifically for periods of user inactivity.

I know there are other apps that only run when user is not using keyboard/mouse and/or during periods of low CPU usage. Anybody know of an easy way to do it?

Thanks!


How about the GetLastInputInfo() from user32.

http://msdn.microsoft.com/en-us/library/ms646302%28VS.85%29.aspx

This will tell you how many ticks since the last time there was some input. You could periodically check this number and pause when it's below a certain threshold.


You could check the CPU performance counter. Yes, I know this does not use native Windows API, but might be ok since you do havea .NET app.

How to get the CPU Usage in C#?


Thanks all for the suggestions. I ended up using a great library - http://www.codeproject.com/KB/cs/uim.aspx - that provides user activity monitoring with several types of underlying methods.

At first I was trying to do this in an underlying windows service. I found out though that GetLastInputInfo() and global system hooks aren't available to windows services. I tried "let this service interact with the desktop" but it didn't help. Just a tip for anyone interested.

I got it working great as a plain ol winforms app. I am going to experiment with recreating it as a screensaver. Could be fun!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜