天天看點

MFC如何建立CPU空閑處理(OnIdle )

about OnIdle 

MFC Library Reference 

CWinApp::OnIdle 

Override this member function to perform idle-time processing.

virtual BOOL OnIdle(

   LONG lCount

);

Parameters

<dl></dl>

<dt>lCount </dt>

<dd></dd>

A counter incremented each time OnIdle is called when the application's message queue is empty. This count is reset to 0 each time a new message is processed. You can use the lCount parameter to determine the relative length of time the application has been idle without processing a message.

 Return Value

<a></a>

Nonzero to receive more idle processing time; 0 if no more idle time is needed.

 Remarks

OnIdle is called in the default message loop when the application's message queue is empty. Use your override to call your own background idle-handler tasks.

OnIdle should return 0 to indicate that no idle processing time is required. The lCount parameter is incremented each time OnIdle is called when the message queue is empty and resets to 0 each time a new message is processed. You can call your different idle routines based on this count.

The following summarizes idle loop processing:

If the message loop in the Microsoft Foundation Class Library checks the message queue and finds no pending messages, it calls OnIdle for the application object and supplies 0 as the lCount argument.

OnIdle performs some processing and returns a nonzero value to indicate it should be called again to do further processing.

The message loop checks the message queue again. If no messages are pending, it calls OnIdle again, incrementing the lCount argument.

Eventually, OnIdle finishes processing all its idle tasks and returns 0. This tells the message loop to stop calling OnIdle until the next message is received from the message queue, at which point the idle cycle restarts with the argument set to 0.

Do not perform lengthy tasks during OnIdle because your application cannot process user input until OnIdle returns.

Note

The default implementation of OnIdle updates command user-interface objects such as menu items and toolbar buttons, and it performs internal data structure cleanup. Therefore, if you override OnIdle, you must call CWinApp::OnIdle with the lCount in your overridden version. First call all base-class idle processing (that is, until the base class OnIdle returns 0). If you need to perform work before the base-class processing completes, review the base-class implementation to select the proper lCount during which to do your work.

 Example

The following two examples show how to use OnIdle. The first example processes two idle tasks using the lCount argument to prioritize the tasks. The first task is high priority, and you should do it whenever possible. The second task is less important and should be done only when there is a long pause in user input. Note the call to the base-class version of OnIdle. The second example manages a group of idle tasks with different priorities.

<a>Copy Code</a>

 Second Example

OnIdle在CWinThread裡面. 虛函數.

 CWinApp派生自CWinThread.

在App類裡面直接寫個

virtual BOOL OnIdle(LONG lCount);