Idle Loop Processing

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Environment:

Introduction

Many applications perform lengthy processing “in the background.” Sometimes, performance considerations dictate using multithreading for such work. Threads involve extra development overhead, so they are not recommended for simple tasks such as the idle-time work that MFC does in the OnIdle function. This article focuses on a simple idle processing technique using the PeekMessage and PumpMessage functions.

Some kinds of background processing are appropriately done 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 idle-time processing by splitting a lengthy process into many small fragments. After processing each fragment, the application yields execution control to Windows by using a PeekMessage loop.

This small code example surrenders execution of the macro so that the operating system can process other events. This function passes control from the application to the operating system.

Some instances in which this function may be useful include the following:

  • Hardware I/O
  • Delay Loops
  • Operating System Calls
  • DDE Deadlocking

Code Manifesto

PeekMessage

The PeekMessage function dispatches incoming sent messages, checks the thread message queue for a posted message, and retrieves the message (if any exist).

PumpMessage

Although the PumpMessage API is undocumented, you can examine its source code in the ThrdCore.Cpp file in MFCSrc relative to your Visual C++ installation.

Using PeekMessage

Here, embed the message loop in one of your functions. This message loop is very similar to MFC’s main message loop, found in CWinThread::Run. That means that such a loop in an application developed with MFC must perform many of the same functions as the main message loop.

void DoEvents()
  {
    MSG dispatch;
    while (::PeekMessage( &dispatch, NULL, 0, 0, PM_NOREMOVE))
      {
        if (!AfxGetThread()->PumpMessage());
      }
  }

The code is embedded in a function named DoEvents(); it loops as long as there is idle processing to do. Within that loop, a nested loop repeatedly calls PeekMessage. As long as that call returns a non-zero value, the loop calls WinThread::PumpMessage to perform normal message translation and dispatching. AFter the inner loop ends, the outer loop performs idle processing with one or more calls. Declare and call DoEvents() from your own function.

One more thing, you might encounter a warning C4390 during compile, asking you to satisfy it with a controlled statement. The remedy is simple; you may add your own control statement inside the block or simply ignore it.

Any good, bad, or ugly feedback will be appreciated. If you think I have left out something or you have even some better ideas, please feel free to e-mail me at asceder@yahoo.com.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read