{"id":35783,"date":"2005-04-26T09:00:18","date_gmt":"2005-04-26T09:00:18","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/04\/26\/thread-messages-are-eaten-by-modal-loops\/"},"modified":"2005-04-26T09:00:18","modified_gmt":"2005-04-26T09:00:18","slug":"thread-messages-are-eaten-by-modal-loops","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050426-18\/?p=35783","title":{"rendered":"Thread messages are eaten by modal loops"},"content":{"rendered":"<p>\nThread messages (as generated by\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/messagesandmessagequeues\/messagesandmessagequeuesreference\/messagesandmessagequeuesfunctions\/postthreadmessage.asp\">\nthe <code>PostThreadMessage<\/code> function<\/a>)\ndo not go anywhere when passed to\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/messagesandmessagequeues\/messagesandmessagequeuesreference\/messagesandmessagequeuesfunctions\/dispatchmessage.asp\">\nthe <code>DispatchMessage<\/code> function<\/a>.\nThis is obvious if you think about it, because there is no window\nhandle associated with a thread message.\n<code>DispatchMessage<\/code> has no idea what to do with a message\nwith no associated window.\nIt has no choice but to throw the message away.\n<\/p>\n<p>\nThis has dire consequences for threads which enter modal loops,\nwhich any thread with a window almost certainly will do at one\ntime or another.\nRecall that the traditional modal loop looks like this:\n<\/p>\n<pre>\nwhile (GetMessage(&amp;msg, NULL, 0, 0)) {\n TranslateMessage(&amp;msg);\n DispatchMessage(&amp;msg);\n}\n<\/pre>\n<p>\nIf a thread message is returned by\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/messagesandmessagequeues\/messagesandmessagequeuesreference\/messagesandmessagequeuesfunctions\/getmessage.asp\">\nthe <code>GetMessage<\/code> function<\/a>,\nit will just fall through the <code>TranslateMessage<\/code>\nand <code>DispatchMessage<\/code> without any action being taken.\nLost forever.\n<\/p>\n<p>\nThread messages are generally to be avoided on threads that\ncreate windows, for this very reason.\nOf course, if you&#8217;re going to create a window, why not use\n<code>PostMessage<\/code> instead, passing that window as the target\nof the posted message?  Since there is now a window handle,\nthe <code>DispatchMessage<\/code> function knows to give the message\nto your window procedure.  Result: Message not lost.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thread messages (as generated by the PostThreadMessage function) do not go anywhere when passed to the DispatchMessage function. This is obvious if you think about it, because there is no window handle associated with a thread message. DispatchMessage has no idea what to do with a message with no associated window. It has no choice [&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":[25],"class_list":["post-35783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Thread messages (as generated by the PostThreadMessage function) do not go anywhere when passed to the DispatchMessage function. This is obvious if you think about it, because there is no window handle associated with a thread message. DispatchMessage has no idea what to do with a message with no associated window. It has no choice [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35783","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=35783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35783\/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=35783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=35783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=35783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}