Bonj said:
Apparently, PostQuitMessage 'posts a quit message to the calling
thread's message queue'. How does it know which window this relates
to? What if the calling thread's message pump is sending messages to
more than one window, or what if there is more than one window in the
calling thread but each with their own message pump?
How would the function know which window to send the quit message to
in both of the above cases?
If you look at almost any other message in these groups, you will see that
it is posted to just a single newsgroup. You, by contrast, seem to post your
every message to at least three groups. Such cross-posting is considered a
breach of newsgroup etiquette since it undermines the principle of having
separate newsgroups for separate topics.
The answer to your question is that the quit message does not relate to a
window at all. It relates to a thread. To quote from the WM_QUIT
documentation:
"The WM_QUIT message is not associated with a window and therefore will
never be received through a window's window procedure."
Instead, WM_QUIT causes GetMessage to return 0, which causes a message loop
of the following (or similar) form
while(GetMessage(...))
to terminate. In the case of PeekMessage, the message is retrieved in the
normal way and the application code should check whether the message is
WM_QUIT and respond appropriately.
Typically a thread has a single message loop regardless of how many windows
it has. The same message loop dispatches messages to all of its windows,
based on the HWND parameter of the message. In the case of WM_QUIT, the
message is never dispatched to a window, the application (or at least the
message pump) simply terminates. The only thing to make sure of is that only
one window calls PostQuitMessage, and it must be the last one destroyed.
Otherwise, the message pump will cease to function prematurely, before all
windows have been properly closed down. This means that only top level
windows should call PostQuitMessage. If there is more than one of them, then
a count should be kept and PostQuitMessage should only be called when there
is only one left.
If it so happens that a thread has more than one message loop (perhaps
because some function doing a lot of processing calls PeekMessage), then
whichever message loop calls GetMessage/PeekMessage first once WM_QUIT
reaches the head of the message queue will be the one to retrieve the
WM_QUIT message. That is how it works with any message. It will then deal
with it in accordance with the code that has been written for that message
loop.