Welcome to MSDN Blogs Sign in | Join | Help

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.

Published Monday, January 12, 2009 7:00 AM by oldnewthing
Filed under:

Comments

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

Monday, January 12, 2009 10:27 AM by John

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.

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

Monday, January 12, 2009 11:40 AM by asf

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]

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

Monday, January 12, 2009 3:54 PM by David Heffernan

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

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

Monday, January 12, 2009 5:11 PM by Mark

"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]

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

Tuesday, January 13, 2009 8:21 AM by Wizou

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]

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

Tuesday, January 13, 2009 9:29 AM by Rafales

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

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

Tuesday, January 13, 2009 5:47 PM by SuperKoko

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

Wednesday, January 14, 2009 11:31 AM by Wizou

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.

New Comments to this post are disabled
 
Page view tracker