Idle processing
Because MFC's application class, CWinApp, provides the message loop that retrieves and dispatches messages, it's a simple matter for CWinApp to call a function in your application when no messages are waiting to be processed If you look at the source code for the CWinThread.: : Run Function That Gets Called by Winmain To Start The Message Loop, You'll See Something That Looks Like this:
Bool bidle = true;
Long lidlecount = 0;
For (;;)
{
While (Bidle &&
!::: PeekMessage (& M_Msgcur, Null, Null, Null, PM_NOREMOVE))
{
IF (! OnIdle (LidleCount ))
BIDLE = FALSE;
}
DO
{
IF (! pumpMessage ())
Return EXITINSTANCE ();
IF (ISIDEMESSAGE))
{
Bidle = True;
LidleCount = 0;
}
} While (:: PepMessage (& M_Msgcur, Null, Null, Null, PM_NOREMOVE));
}
Before it calls PumpMessage to retrieve and dispatch a message, Run calls :: PeekMessage with a PM_NOREMOVE flag to check the message queue. If a message is waiting, :: PeekMessage copies it to an MSG structure and returns a nonzero value but does not . remove the message from the queue If no messages are waiting, :: PeekMessage returns 0. Unlike :: GetMessage, :: PeekMessage does not wait for a message to appear in the message queue before returning;. it returns immediately If :: PeekMessage returns nonzero, indicating that messages are waiting to be processed, CWinThread :: Run enters a do-while loop that calls CWinThread :: PumpMessage repeatedly to retrieve and dispatch the messages. But if :: PeekMessage returns 0 and the bIdle flag is set , CWinThread :: Run calls a member function named OnIdle to give the application an opportunity to perform idle processing. Because OnIdle is a virtual function and because CWinApp is derived from CWinThread, a derived application class can hook into the idle lo Op by replacing cwinapp :: onIdle with an onidle function of its oow.
Back in the days of Windows 3.x, when applications were inherently single-threaded, OnIdle was the perfect place to perform background processing tasks such as print spooling and garbage collecting. In 32-bit Windows, CWinApp :: OnIdle's usefulness is greatly diminished because low-priority tasks can be performed more efficiently in background threads of execution. OnIdle still has legitimate uses, however. MFC uses it to update toolbar buttons and other always-visible user interface objects by calling update handlers registered in the message map. It .......................
When you call FromHandle to convert a window handle into a CWnd pointer, MFC consults an internal table called a handle map that correlates CWnd objects and window handles. If it finds the handle it's looking for, MFC returns a pointer to the corresponding CWnd object. If the window handle does not appear in the handle map because a corresponding CWnd does not exist, however, FromHandle creates a temporary CWnd object and returns its address to the caller. The next time OnIdle is called (which does not occur until after the message handler that called FromHandle returns), MFC cleans up by deleting the temporary CWnd object. that's why the documentation for some MFC functions warns that returned pointers might be temporary and "should not be stored for later use." What that really means is that an object referenced by one of these temporary pointers is not guaranteed to exist outside the scope of the current message handler because, once that handler returns, OnIdle is liable to be called-a Nd the object deled-at any moment.using onIdle
AN MFC Application Can Enact Its Own Idle-Processing Regimen by Overriding The Virtual Onidle Function That IT ITS from CWINApp. Onder Is Prototyped AS Follows:
Virtual Bool Onidle (long lcount)
lCount is a 32-bit value that specifies the number of times OnIdle has been called since the last message was processed. The count continually increases until the message loop in CWinThread :: Run calls PumpMessage to retrieve and dispatch another message. The count is then reset to 0 and starts again. WM_PAINT messages, WM_SYSTIMER messages, and certain mouse messages do not cause lCount to be reset. (WM_SYSTIMER is an undocumented message Windows uses internally.) lCount can be used as a rough measure of the time elapsed since the last message or of the length of time the application has been idle. If you have two background tasks you'd like to perform during idle time, one that's high priority and another that's low, you can use lCount to determine when to execute each task. For example, you might perform the high-priority task each time lCount reaches 10 and the low-priority task when lCount hits 100 or even 1,000.If you could log the calls to an application's OnIdle function without slowing it down, you'd find that 1,000 is not all that high a number. Typically, OnIdle is called 100 or more times per second when the message queue is empty, so a low-priority background task that kicks off when lCount reaches 1,000 is typically executed when the mouse and keyboard are idle for a few seconds. A high-priority task that begins when lCount reaches 10 is executed much more often because the count frequently reaches or exceeds 10, even when the message loop is relatively busy. Idle Processing Should Be Carried Out As Quickly As Possible Because Message Traffic Is Blocked Until OnIdle Returns.
The value that OnIdle returns determines whether OnIdle will be called again. If OnIdle returns a nonzero value, it's called again if the message queue is still empty. If OnIdle returns 0, however, further calls are suspended until another message finds its way into the message queue and the idle state is reentered after the message is dispatched. The mechanism that makes this work is the bIdle flag in CWinThread :: Run, which is initially set to TRUE but is set to FALSE if OnIdle returns FALSE. The while loop that calls OnIdle tests the value of bIdle at the beginning of each iteration and falls through if bIdle is FALSE. bIdle is set to TRUE again when a message shows up in the message queue and PumpMessage is called. As a practical matter, you can save a few CPU cycles by returning FALSE from OnIdle if your background processing is complete for the moment and you do not want OnIdle to be called again until the flow of messages resumes. Be careful, however, not to return FALSE before th e framework has finished its most recent spate of idle-processing chores and thus deprive it of the idle time it needs.The cardinal rule to follow when using OnIdle is to call the base class version of OnIdle from the overridden version. The following OnIdle override Demonstrates The Proper Technique. The base Class's Onidle Function IS Called First, And After The Call Returns, The Application Performs Its Own Idle Processing:
Bool CmyApp :: OnIdle (long lcount)
{
CWINAPP :: OnIdle (LCOUNT);
Doidlework (); // do custom idle processing.
Return True;
}
It turns out that the framework does its processing when lCount is 0 and 1. Therefore, an even better approach is to accord higher priority to the framework's OnIdle handler by delaying the start of your own idle processing until lCount reaches a value of 2 or higher : BOOL CMYAPP :: OnIdle (long lcount)
{
CWINAPP :: OnIdle (LCOUNT);
IF (LCOUNT == 2)
Doidlework (); // do custom idle processing.
Return True;
}
You can see for yourself What mfc does during idle time by examining the source code for cinthread :: OnIdle in twrdcore.cpp and cwinapp :: onIdle in appcore.cpp.
Because the OnIdle implementations in the previous paragraph always returns TRUE, calls to OnIdle will continue unabated even if both you and the framework are finished with OnIdle for the time being. The following OnIdle override reduces overhead by returning FALSE when both MFC's idle processing and the Application's iDLE Processing Are Complete:
Bool CmyApp :: OnIdle (long lcount)
{
Bool bcontinue = cwinapp :: onIdle (lcount);
IF (LCOUNT == 2)
Doidlework (); // do custom idle processing.
Return (bcontinue œœ lcount <2);
}
The fact that application-specific idle processing is not started until lCount equals 2 means that the framework will not be deprived of the idle time it needs if the application's OnIdle function returns FALSE.
It's important to perform idle processing as quickly as possible to avoid adversely impacting the application's responsiveness. If necessary, break up large OnIdle tasks into smaller, more manageable pieces and process one piece at a time in successive calls to OnIdle. The following OnIdle function begins ITS Work WHEN LCOUNT REACHES 2 and Continues Responding to Onidle Calls Until Doidlework Returns 0: Bool CmyApp :: OnIdle (long lcount)
{
Bool BmfcContinue = CWINAPP :: OnIdle (LCOUNT);
Bool bappContinue = true;
IF (LCOUNT> = 2)
BAPPCONTINUE = doidlework (); // do custom idle processing.
Return (BMFcContinue œœ);
}
BECAUSE DoiDell's Return Value Is Also Used As Onder's Return Value, OnIdle Will Cease to Be Called Once Doidlework Has Completed ITS Appointed Task.
Idle processing vs. multithreading
In Chapter 17, you'll learn about another way to perform background tasks that involves multiple threads of execution Multithreading is a powerful programming paradigm that's ideal for performing two or more tasks in parallel It's also scalable:.. On a multiprocessor system containing n CPUs , Windows NT and Windows 2000 will execute up to n threads concurrently by scheduling each to run on a different processor. (Windows 95 and Windows 98, by contrast, force all threads to share a single CPU, even on multiprocessor systems.)
Given the Robust Multithreading Support in 32-Bit Windows, IT's Reasonable To ask, IF at all, you shouth us, iF at all, you shouthready. Here. Here. Here. Here. Here....
When you have background tasks to perform that must execute in the application's primary thread. User interface_related tasks tend to be very thread-sensitive. That's one reason why MFC performs user interface updates in the primary thread. When you have background tasks to perform and the application that you're writing must work in 16-bit Windows as well as in 32-bit Windows. Multithreading is not supported in 16-bit Windows.In these situations, performing background tasks in OnIdle makes a lot of sense. Under any other Circumstances, Multithreading is in all likelihood the properties.