Thread message pump

318 Views Asked by At

I want to get a working understanding of how message pumping works in the Windows system. I created a console application. I cteated new thread in this aplication:

var thread = new Thread(MessagePump) {IsBackground = true};
 _thread.Start();

The message pumping looks like this:

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

I send messages to this thread using another program. I see that the thread is receiving my messages. I want to understand how this happens. I have not created a window for this thread. According to the instructions, you need a window to receive messages.

1

There are 1 best solutions below

0
On

The rules for when a thread can receive messages are spelled out:

The system creates a thread's message queue when the thread makes its first call to one of the User or GDI functions.

A window is not needed for a thread to receive messages. As the documentation explains, messages posted to a thread are not associated with a window and follow slightly different rules.