c++winapiloopsmessagemessage-pump

Win32 message pump, does DispatchMessage() handle entire message queue or just top message?


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
}

Solution

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