{"id":19743,"date":"2008-12-23T10:00:00","date_gmt":"2008-12-23T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/12\/23\/why-isnt-there-a-sendthreadmessage-function\/"},"modified":"2008-12-23T10:00:00","modified_gmt":"2008-12-23T10:00:00","slug":"why-isnt-there-a-sendthreadmessage-function","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20081223-00\/?p=19743","title":{"rendered":"Why isn&#8217;t there a SendThreadMessage function?"},"content":{"rendered":"<p>Here&#8217;s an interesting customer question:<\/p>\n<blockquote class=\"m\"><p> Windows has <code>PostMessage<\/code> and <code>SendMessage<\/code>. It also has <code>PostThreadMessage<\/code> but no <code>SendThreadMessage<\/code>. Why isn&#8217;t there a <code>SendThreadMessage<\/code> function? Am I forced to simulate it with an event? <\/p><\/blockquote>\n<p> What would this imaginary <code>SendThreadMessage<\/code> function do? Recall that <code>SendMessage<\/code> delivers the message directly to the window procedure; the message pump never sees it. The imaginary <code>SendThreadMessage<\/code> function would have to deliver the message directly to&#8230;. what? There is no &#8220;thread window procedure&#8221; to deliver it to. <\/p>\n<p> Okay, maybe you still intend to process the thread message in your message pump, but you want the caller of the imaginary <code>SendThreadMessage<\/code> function to wait until you&#8217;ve finished processing the message. But how does it know when you&#8217;re finished? It can&#8217;t wait for <code>DispatchMessage<\/code> to return, since <code>DispatchMessage<\/code> can&#8217;t dispatch thread messages. (Where would it dispatch them <i>to<\/i>?) The processing of the thread message is completely under the control of the message pump. The window manager gives it a thread message, and as far as the window manager is concerned, that&#8217;s the end of the story. <\/p>\n<p> You might say that the processing of the thread message is complete when somebody next calls <code>GetMessage<\/code> or <code>PeekMessage<\/code>, but there&#8217;s no guarantee that the next call to a message-retrieval function will come from the message pump. Handling the thread message may result in a call to <code>MessageBox<\/code>, and as a modal function, it will have its own message loop, which will call <code>GetMessage<\/code>, resulting in your imaginary <code>SendThreadMessage<\/code> function deciding that message processing is complete when in fact it&#8217;s still going on. <\/p>\n<p> What should you do instead? Just create a window and send it a message. The scenarios where you would want to use the <code>PostThreadMessage<\/code> function are very limited and specialized. Under normal circumstances, you should just send a regular window message. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s an interesting customer question: Windows has PostMessage and SendMessage. It also has PostThreadMessage but no SendThreadMessage. Why isn&#8217;t there a SendThreadMessage function? Am I forced to simulate it with an event? What would this imaginary SendThreadMessage function do? Recall that SendMessage delivers the message directly to the window procedure; the message pump never sees [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[26],"class_list":["post-19743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>Here&#8217;s an interesting customer question: Windows has PostMessage and SendMessage. It also has PostThreadMessage but no SendThreadMessage. Why isn&#8217;t there a SendThreadMessage function? Am I forced to simulate it with an event? What would this imaginary SendThreadMessage function do? Recall that SendMessage delivers the message directly to the window procedure; the message pump never sees [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/19743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=19743"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/19743\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=19743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=19743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=19743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}