0

So i've been reading up on the Win32 message pump and I was curious if the DispatchMessage() function deals with the entire message queue, or just the message at the top of the queue?

For example, i've seen loops such as:

while(true) 
{

    MSG  msg;

    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        if (msg.message == WM_QUIT) 
        {
            break;
        } 
        else 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    } 

    do 
    {   
    } while (clock.getTimeDeltaMilliseconds() < 1.66f); // cap at 60 fps

    // run frame code here
}

In this example would every message get processed or does this loop structure cause only one message to be processed per frame?

If it does deal with only one message at a time, should I change the if(PeekMessage) statement to a while loop to ensure all messages are handled?:

while(true) 
{

    MSG  msg;

    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        if (msg.message == WM_QUIT) 
        {
            return;
        } 
        else 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    } 

    do 
    {   
    } while (clock.getTimeDeltaMilliseconds() < 1.66f); // cap at 60 fps

    // run frame code here
}
kbirk
  • 3,906
  • 7
  • 48
  • 72
  • I haven't done Windows development in _years_. Why is the frame timing code in the same thread as the message loop? – Chad Oct 26 '12 at 18:11

2 Answers2

3

It only deals with the top message. MSG is a structure that holds information about one message, filled when you call GetMessage or PeekMessage, the former being a blocking function. You then pass that information about the one message to DispatchMessage.

If you want to handle the entire message queue before whatever else you do in the loop, you should enclose that part in a loop.

chris
  • 60,560
  • 13
  • 143
  • 205
  • So I could use the statement while(PeekMessage(...)) in my second exmplae to iterate through each message in the queue? Is this a recommended approach? – kbirk Oct 26 '12 at 18:08
  • @Pondwater, You'd have to enclose the `TranslateMessage` and `DispatchMessage` calls within the loop as well, so that every message pulled from the queue is actually handled, not just the last. I've seen the loop there before, and I'd say it's logical, because lagging the user interface is generally a bad idea. – chris Oct 26 '12 at 18:10
2

DispatchMesasge has nothing to do with message queue, it only process the message that you pass to it, actual function that remove message from message queue is PeekMessage and it only remove or peek one message, so you are right you should use a while loop to process all messages in the queue

BigBoss
  • 6,904
  • 2
  • 23
  • 38