Microsoft KB Archive/99999

= INFO: Background Processing in an MFC Application =

Article ID: 99999

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 6.1

 Microsoft Visual C++ 6.0 Professional Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 5.0 Learning Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q99999

<div class="summary_section">

SUMMARY
Many applications perform lengthy processing &quot;in the background&quot; during intervals when the user is not otherwise interacting with the application. In an application developed for the Microsoft Windows operating system, an application can perform background processing by splitting a lengthy process into many small fragments. After processing each fragment, the application yields execution control to Windows using a PeekMessage loop.

An application developed with the Microsoft Foundation Class Library can perform background processing either by using the PeekMessage loop in the library code's main message loop or by embedding another PeekMessage loop in the application.

NOTE: The remainder of this article, namely the sample code, is specific to Visual C++ for Windows and Visual C++ 32-bit Edition, version 2.x. For Visual C++ 32-bit Edition, version 4.0, please refer to the Frequently Asked Question (FAQ) about MFC &quot;Background Processing in an MFC Application&quot; in the Visual C++ 4.0 Books Online.

<div class="moreinformation_section">

MORE INFORMATION
In an application developed with the Microsoft Foundation Class Library, the main message loop in the CWinApp class contains a PeekMessage loop. This loop calls the CWinApp::OnIdle function between messages. An application can process messages in this idle time by overriding the OnIdle function. For more information about performing background processing in the OnIdle function, please refer to the documentation for the CWinApp::OnIdle function in the &quot;Class Libraries Reference&quot; manual.

Another method to perform background processing in an application involves embedding a PeekMessage loop in a function. Because a PeekMessage loop is very similar to the main message loop, such a loop in an application developed with the Microsoft Foundation Class Library must perform many of the functions of the main message loop in the library. The following code fragment demonstrates writing a PeekMessage loop that is compatible with the Microsoft Foundation Class Library:

while (bDoingBackgroundProcessing) {     while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {        if (msg.message == WM_QUIT) {           bDoingBackgroundProcessing = FALSE; ::PostQuitMessage(nExitCode); break; }        if (!AfxGetApp->PreTranslateMessage(&msg)) {           ::TranslateMessage(&msg); ::DispatchMessage(&msg); }     }      AfxGetApp->OnIdle(0);   // updates user interface AfxGetApp->OnIdle(1);  // frees temporary objects

// Perform some background processing here // When you are done with the background task // set bDoingBackgroundProcessing to FALSE }

Keywords: kbinfo KB99999

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.