January 25th, 2006

You can call MsgWaitForMultipleObjects with zero handles

There is no WaitMessageTimeout function, but you can create your own with the assistance of the MsgWaitForMultipleObjects function.

BOOL WaitMessageTimeout(DWORD dwTimeout)
{
 return MsgWaitForMultipleObjects(
    0, NULL, FALSE, dwTimeout, QS_ALLINPUT)
      == WAIT_TIMEOUT;
}

To wait for a message with timeout, we use the MsgWaitForMultipleObjects in a vacuous sense: You pass it a list of objects you want to wait for, as well as a timeout and a set of queue states, asking that the function return when any of the objects is signalled or when a message is ready. By passing no objects, the only thing left to wait for is an incoming message.

Next time, we’ll see how this basic idea can be used to build a slightly more complex function.

[1/26: Fix call to MsgWaitForMultipleObjects; had it confused with MsgWaitForMultipleObjectsEx. That’s what happens when you write entries on an airplane.]

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.