the different types of message loop

Dec 6, 2012 at 3:16am
first I handle the message loop like this:
1
2
3
4
MSG msg;
while(GetMessage(&msg, 0, 0, 0)) {
    // do stuff
}


after I learn the double buffering, handle like this:

1
2
3
4
5
6
7
8
MSG msg;
while(msg.message != WM_QUIT) {
    if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
        // translate and dispatch as normal
    } else {
        // do frame stuff
    }
}


I'm wondering why in the double buffering don't use GetMessage() function, can
msg.message != WM_QUIT
and
GetMessage(&msg, 0, 0, 0)
in each loop exchange each other?
Dec 6, 2012 at 8:55pm
The real difference here is between the PeekMessage and the GetMessage function behaviour

When GetMessage checks the message queue, if there are no messages it waits for a message to become available.

When PeekMessage checks the queue, if there is a message available then PeekMessage returns it, if there is no message available then PeekMessage
returns anyway.

So GetMessage will always return with a message even if it has to wait for a long long time.

PeekMessage does not wait if there is no message available in the queue - it will return with or without a message.
So with PeekMessage you need to check if it got a message
using if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) //check if PeekMessage found a message in the queue


I could go on... but I will point you to this link:(Using Messages and Message Queues)
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms644928%28v=vs.85%29.aspx



Topic archived. No new replies allowed.