bodacydo
bodacydo

Reputation: 79509

Does a win32 application have one message loop? Or is it one message loop per window?

I'm a little bit confused about how message loops work in win32 programming. In my WinMain I always put the following:

while ( GetMessage ( &msg, NULL, 0, 0 ) > 0 )
{
    TranslateMessage ( &msg );
    DispatchMessage ( &msg );
}

This is a while loop that pretty much runs until your application stops. Does that mean you have one message loop per application rather per window?

Upvotes: 14

Views: 5486

Answers (1)

xanatos
xanatos

Reputation: 111940

From About Messages and Message Queues:

Applications with multiple threads can include a message loop in each thread that creates a window.

Note that a message queue CAN support multiple windows... The second parameter of GetMessage is the handle of the window you want to watch messages for. If NULL then all the windows of the thread.

As a second note, it is possible to create a message queue without windows (at least from Windows 2000 onward). It is described in the documentation for PostThreadMessage:

In the thread to which the message will be posted, call PeekMessage as shown here to force the system to create the message queue.

PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)

Upvotes: 15

Related Questions