-2

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 2
    How are you "sending messages to this thread"? – Raymond Chen Sep 09 '21 at 12:53
  • `var Thread = new Thread` suggests C#. The message pump loop you quote is C++. I supppse you're confused because you're mixing different langauges, which work in different ways. – MSalters Sep 09 '21 at 12:53
  • If you don't have a window then you must be using PostThreadMessage(). Which was designed for this usage. And must never be used if the thread owns any visible window, messages get lost when the thread enters a modal message loop (e.g. resizing the window). – Hans Passant Sep 09 '21 at 13:03

1 Answers1

0

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.

IInspectable
  • 46,945
  • 8
  • 85
  • 181