How does PostQuitMessage know which thread to post the quit message to?


Commenter bav016 asks how functions like PostQuitMessage and SetTimer(NULL) know which thread the messages should go to. Unlike some functions such as InvalidateRect which have a window handle parameter that lets you say which window you want to operate on, PostQuitMessage and SetTimer(NULL) don't say which thread the WM_QUIT or WM_TIMER message should go to. How do they decide?

The messages go to the current thread; that is, they are delivered to the thread that called the function in the first place.

There are many functions which operate on an implicit message queue, and those cases, they operate on the message queue associated with the thread making the call. If you call GetKeyState you retrieve the calling thread's keyboard state. If you call GetMessage you retrieve messages from the calling thread's message queue. If you call InSendMessage, you are told about the calling thread's message processing state. If you call GetQueueStatus you retrieve information about the calling thread's msesage queue. You get the idea.

If you want these functions to operate on a thread different from the one that is executing, you'll have to ask that thread to make the call for you.

Comments (8)
  1. John says:

    Not to nail bav016 specifically, but isn’t this stuff supposed to be blindingly obvious?  I’ve seen a number of articles here recently where the end result should be obvious.  I mean this stuff is basic Windows fundamentals.  I am normally against the whole "RTFM" thing, but come on.

  2. asf says:

    When working on small utils and trying to keep the size down, I just use plain PostMessage to my window, seems to work fine and one less item in the import table

    [But be aware that it’s not the same thing. See my book for more details. -Raymond]
  3. David Heffernan says:

    I bet using PostMessage to avoid importing PostQuitMessage makes a large difference in the exe size.

  4. Mark says:

    “If you want these functions to operate on a thread different from the one that is executing, you’ll have to ask that thread to make the call for you”.

    How do you ask other threads to make that call? Also somewhat related, I would like to know whether or not the message queue of a certain thread (other than my own) empty or not at any given time. How do I do that from within my own application? I know I can use GetQueueStatus() to ask about my own queue status, though. Could you please shed more light on this? Thanks.

    [How you communicate with other threads is a private matter between you and that other thread. -Raymond]
  5. Wizou says:

    What if the current thread doesn’t have a thread queue yet ? (ie: did not call any of GetMessage & such functions…)

    [http://search.live.com/results.aspx?q=site:blogs.msdn.com/oldnewthing+thread+queue. -Raymond]
  6. Rafales says:

    I think every thread got his thread queue from the start.

  7. Wizou says:

    Indeed, I was thinking about that article..

    PostQuitMessage could behave like PostThreadMessage and fail, or could create the queue on-demand…

    After re-reading the previous article carefully, I assume it will create the queue.

Comments are closed.

Skip to main content