English 中文(简体)
Identifying idle state on a windows machine
原标题:

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?

最佳回答

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.

问题回答

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





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签