Contents of the Q71670.DOC file
Q71670 INF: Background Processing with PeekMessage() Code Example
Microsoft Windows Software Development Kit (WINSDK)
BACKGRND is a sample program in the Software/Data Library that
demonstrates how to use a PeekMessage() loop to do background
processing in a Windows application. BACKGRND can be found in the
Software/Data Library by searching on the word BACKGRND, the Q number
of this article, or S13017. BACKGRND was archived using the PKware
BACKGRND uses a PeekMessage() loop to do background processing. The
background task is to draw random rectangles. One "batch" of
rectangles is drawn each time through the PeekMessage() loop, and all
of the rectangles in each batch are drawn in the same color. This
provides a visual indication of how much background work is being done
before BACKGRND offers to yield.
Both the "batch size" and the total number of rectangles to be drawn
can be changed via the menu.
BACKGRND does not replace the main GetMessage() loop with a
PeekMessage() loop. Instead, it enters a PeekMessage() loop only when
the user chooses the Draw! menu item.
While drawing the rectangles, BACKGRND puts up a modeless dialog box
1. Allows the user to cancel the background processing before all of
the rectangles have been drawn (by choosing the Cancel button)
2. Indicates both the total number of rectangles to be drawn and the
3. Displays a "gas gauge" that indicates the percentage of the
background job that has been completed
BACKGRND can be run along with another Windows application (for
example, Notepad) to illustrate the difference between "polite" and
"impolite" background processing.
If the batch size is set to a "polite" small value [for example, 10
rectangles each time through the PeekMessage() loop], the user can
easily type within Notepad while BACKGRND is drawing rectangles.
BACKGRND draws rectangles in the spare time slices between the
messages generated by the user's keystrokes, and calls PeekMessage()
often enough that the user will still find Notepad to be responsive.
If the batch size is set to an "impolite" large value [for example,
1000 rectangles each time through the PeekMessage() loop], BACKGRND
can complete its background job much more quickly, because there are
fewer calls to PeekMessage() and thus less overhead. However, a user
trying to type into Notepad will find the keyboard so unresponsive
that Notepad will be essentially useless.
COPYRIGHT Microsoft Corporation, 1991.
Updated 91/05/24 03:44