views:

246

answers:

2

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn't mean the system has been idle for 10 minutes - scans, downloads, movies - lots of reasons.

So how can we identify whether the system is truly idle or not?

Does anyone know what Window's definition of "idle" is? Must be a combination of thresholds - like less than 5% cpu util, less than 3% disk util etc. - along with the user input parameter...anyone knows the exact definition?

+2  A: 

There is no true definition of "idle". It would be whatever you would want it to be.

Daniel A. White
What is the Windows' definition? Or what does Microsoft want it to be? :)
SaM
I don't think there is any hard fast rules. It depends on the application.
Daniel A. White
+1  A: 

Since most Windows apps are UI driven, and Windows UI is driven by fetching messages from the message queue, one common way to define when an application is "idle" is when it has no messages in its message queue.

There are always messages flying around in a Windows app (user generated and system generated), but you will see frequent short idle periods in a typical app. When the mouse moves over a window in your app, your idle periods will drop to near zero until the mouse stops moving because mouse activity generates a lot of message activity.

So, in your Windows app's message loop, if you are currently using GetMessage you will need to change your loop to use PeekMessage instead. GetMessage blocks when the message queue is empty until a message arrives. PeekMessage returns true if there is a message in the queue or false if there is none. With flags you can make PeekMessage get the message in the same call.

When PeekMessage returns false, you can divert execution to your "idle" task instead of waiting for the next message.

In WinForms applications, use the Application.Idle event

Don't do too much in your idle code though, or your UI will feel sluggish because messages aren't being processed quickly. Overuse of the idle cycle will also make your CPU run hot and drain your laptop battery.

dthorpe