I'm trying to understand the difference between MsgWaitFor and WaitFor functions.
1) I understand that the MsgWaitFor is running under a message loop, while the WaitFor does not?
2) Does the MsgWaitFor functions is better for an application that need to receive a sequence of events in a row? Does Windows queues the messages, so the application won't miss any events?
Say application wants to receive event A and B which happens frequently.
The application will open a thread:
while (1) {
ret = WaitForMultipleObjects(...); // wait for events A and B
if (ret == WAIT_OBJECT_0) {
process_event();
}
}
The question is, when the thread is busy with processing, meaning it is currently not blocked by WaitForMultipleObjects. How can the thread avoid missing the events until it goes back to waiting?
The main difference is
MsgWaitForMultipleObjects/MsgWaitForMultipleObjectsExcan wait for messages (in message queue) in addition to the following object types:So if you have a thread that creates windows, use
MsgWaitForMultipleObjectsorMsgWaitForMultipleObjectsEx, rather thanWaitForMultipleObjectsEx.Message has queue and the queue has a length (10,000 posted messages). So when processing messages too slow, the
PostMessagemay fails withERROR_NOT_ENOUGH_QUOTA. But for receiver side, you will not miss messages and you can handle queued messages one by one.Event object has no queue and it has two state:
signaledandnonsignaled. Setting an event that is already set has no effect. So if this is a manual-reset event object, it remainssignaleduntil it is set explicitly to thenonsignaledstate by theResetEventfunction. There may be a miss at setting side instead of checking side.