You can call MsgWaitForMultipleObjects with zero handles

Raymond Chen

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.]


Discussion is closed.

Feedback usabilla icon