Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

winapi

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?

like image 823
bodacydo Avatar asked Aug 07 '13 18:08

bodacydo


People also ask

What is message loop in windows programming?

The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows. Windows programs that have a GUI are event-driven. Windows maintains an individual message queue for each thread that has created a window. Usually only the first thread creates windows.

What is the use of message loop?

A message loop allows PPAPI calls to be issued on a thread. You may not issue any API calls on a thread without creating a message loop. It also allows you to post work to the message loop for a thread.

What is message loop for a thread?

An event loop, or sometimes called a message loop, is a thread that waits for and dispatches incoming events. The thread blocks waiting for requests to arrive and then dispatches the event to an event handler function. A message queue is typically used by the loop to hold incoming messages.

What is a message loop in programming?

The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows . Windows programs that have a GUI are event-driven. Windows maintains an individual message queue for each thread that has created a window.

Why does the application need a loop to retrieve messages?

The application needs a loop to retrieve the messages and dispatch them to the correct windows. For each thread that creates a window, the operating system creates a queue for window messages. This queue holds messages for all the windows that are created on that thread.

How to dispatch a message in Win32 message loop?

To dispatch the message, Win32 provides an API function, DispatchMessage (). The syntax of the function looks like below: Where lpMsg is a pointer to the valid MSG structure. Usually, we pass the MSG structure which is received from GetMessage () method, through this function. Step 3. Are we done with our Message Loop? Not really.

What is an unsourced message loop?

Unsourced material may be challenged and removed. The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows . Windows programs that have a GUI are event-driven. Windows maintains an individual message queue for each thread that has created a window.


Video Answer


1 Answers

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)
like image 88
xanatos Avatar answered Nov 01 '22 00:11

xanatos