Nonpreemptive Multitasking

The GetMessage call within the message loop is important for another reason. Except for some device drivers that must process hardware interrupts (such as the timer, keyboard, mouse, and serial port), Windows usually treats HELLOWIN as if it were the only program running under the system. Windows will not arbitrarily switch away from HELLOWIN and run some other program. The exception is during the GetMessage call. If HELLOWIN's message queue has no waiting messages and another program has some messages in its message queue, then Windows switches from HELLOWIN to the other program. That makes sense, does it not?

You can think of it this way: In most cases, when your program calls a function in Windows, you can expect that the function will be processed and return control to your program within a reasonable period of time. When you call GetMessage, however, it may be some time before Windows returns with a message if the program's message queue does not contain any messages and another program's message queue does. Windows can take advantage of the delay caused by an empty message queue during a GetMessage call to switch to another program that has messages waiting. As a result, Windows has a ”jumpy“ type of multitasking. Sometimes a program has a long job to do, and all other programs running under Windows seem to stop running during this time.

Rather than ”jumpy multitasking,“ this characteristic is usually called ”nonpreemptive multitasking.“ Windows is multitasking between programs by switching between them. But Windows is not doing this as it is done within a traditional multitasking system, based on the tick of a hardware clock and allocating each program a tiny time-slice to do its stuff. It's multitasking at the point where programs check the message queue for messages.

The process is actually a little more complex than that: Windows also switches between programs during PeekMessage and WaitMessage calls, but these are less common than GetMessage. Furthermore, the WM_PAINT and WM_TIMER messages are treated as low-priority messages, so Windows can switch from a program if only WM_PAINT and WM_TIMER messages are present in the queue.