DOC: MFC Application Idle Processing and Power Consumption

ID: Q93915


The information in this article applies to:


SUMMARY

The Microsoft Foundation Classes (MFC) provide a virtual member function CWinApp::OnIdle() to support background or idle-time processing when an application is not receiving messages. Because the OnIdle() function uses the ::PeekMessage() function in Microsoft Windows, it is important to realize how using the OnIdle function affects other applications in the Microsoft Windows environment and affects power consumption on battery powered machines that run Windows.


STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++ for Windows, version 1.51, and Visual C++, 32-bit Edition, version 2.0.


MORE INFORMATION

The CWinApp class is designed to support a "power-friendly" application while allowing the application to perform background processing when it is idle (not receiving any messages). A power-friendly application uses the PeekMessage() and Yield() functions to perform temporary background tasks. A power-friendly PeekMessage() loop exits when background processing is complete because, while an application is in a PeekMessage() loop, Windows cannot go idle.

The default CWinApp::OnIdle() function deletes temporary objects and returns the value FALSE to indicate that processing is complete and that the application can "sleep." The FALSE return value causes the application to exit the "PeekMessage() and OnIdle()" loop in the CWinApp::Run() function. As processing continues, Run() calls GetMessage() that either retrieves a message from the application's message queue, or, in this case, yields control until a message becomes available. When all applications in the system yield control, a battery powered computer can conserve its power.

Execution continues in the "PeekMessage() and OnIdle()" loop only as long as no messages are present in the application's message queue and OnIdle() returns TRUE. This allows the system to remain active as long as the application has OnIdle() processing to perform. On each iteration of the loop, the Run() function increments the lCount parameter before passing it to OnIdle(). OnIdle() can use the value of lCount to determine when to return FALSE. Doing so allows the application to defer idle-time processing until after the application receives additional user input.

When execution returns from PumpMessage(), the value of lCount is reset to zero. On page 637 of the Microsoft C/C++ version 7.0 "Class Libraries Reference" manual, the documentation for the CWinApp::OnIdle() member function incorrectly states that lCount is incremented each time GetMessage() finds the message queue to be empty. The statement in the documentation should read as follows:

lCount
A counter incremented each time PeekMessage finds the message queue empty. This count is reset to 0 each time a new message is processed. lCount can be used to determine relatively how long the application has been idling without processing a message.
If an application overrides the CWinApp::OnIdle() function, three conditions must be met:

Additional query words: 7.00 1.00 1.50 2.50 backproc DeleteTempMap noupdate


Keywords          : kbdocfix kbMFC KbUIDesign kbVC 
Version           : 7.00 | 1.00 1.50 | 1.00
Platform          : MS-DOS NT WINDOWS 
Issue type        : 

Last Reviewed: August 8, 1999