{"id":3313,"date":"2013-09-05T07:00:00","date_gmt":"2013-09-05T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/09\/05\/why-are-my-posted-messages-getting-lost-when-the-user-drags-my-window-around\/"},"modified":"2013-09-05T07:00:00","modified_gmt":"2013-09-05T07:00:00","slug":"why-are-my-posted-messages-getting-lost-when-the-user-drags-my-window-around","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130905-00\/?p=3313","title":{"rendered":"Why are my posted messages getting lost when the user drags my window around?"},"content":{"rendered":"<p>\nThis question was inspired by an actual customer question,\nbut I changed a lot of it around to make for a more interesting story.\n(Trust me, the original story was <i>even more boring<\/i>.)\n<\/p>\n<p>\nA customer&#8217;s background thread posted a message to the main UI thread\nto signal something (details not important).\nThey found that the posted message was never received if the user was\nin the process of dragging the main window around at the time the message\nwas posted.\nWhy would dragging a window cause posted messages to be lost?\n&#8220;We used to post a thread message, but then we saw that\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/04\/26\/412116.aspx\">\n<i>thread messages are eaten by modal loops<\/i><\/a>,\nso we switched to posting a message, as you recommended.\nBut that didn&#8217;t help.&#8221;\n<\/p>\n<p>\nDragging a window doesn&#8217;t cause messages to be lost.\nThe modal loop\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/02\/21\/377337.aspx\">\ncreated by the window dragging code<\/a>\ncalls\n<code>Dispatch&shy;Message<\/code>\nto deliver the posted message to its target window&#8217;s window procedure.\n<\/p>\n<p>\n&#8220;Oh, we don&#8217;t handle the message in the window procedure.\nWe process it here:\n<\/p>\n<pre>\nBOOL MyApp::PreTranslateMessage(MSG *pmsg)\n{\n  if (pmsg-&gt;message == OUR_SPECIAL_MESSAGE) {\n    ... special code here ...\n    return TRUE; \/\/ handled\n  }\n  return FALSE; \/\/ not handled\n}\n<\/pre>\n<p>\nCould that be the problem?&#8221;\n<\/p>\n<p>\nYes, that&#8217;s the problem.\n<\/p>\n<p>\nThe customer saw the recommendation to use <code>Post&shy;Message<\/code>\ninstead of <code>Post&shy;Thread&shy;Message<\/code> but simply\nblindly followed the advice rather than\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2009\/11\/04\/9917052.aspx\">\nunderstanding its rationale<\/a>\nso they could apply the advice correctly.\n<\/p>\n<p>\nIf you read\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/04\/26\/412116.aspx\">\nthe original recommendation<\/a>,\nyou&#8217;ll see that the problem is that when a modal loop runs,\nyour message loop is no longer in control,\nand therefore any customizations you&#8217;ve made to your message loop\nwill not be in effect.\nThis is normally a good thing.\nFor example, if a dialog box calls <code>Message&shy;Box<\/code>,\nthe dialog keyboard shortcuts shouldn&#8217;t be active while the message box\nis displayed.\nIt would be very strange if hitting <kbd>Enter<\/kbd> caused the\ndialog box to invoke its default button\n<i>while the modal message box is still on the screen<\/i>.\nThe result would most likely be\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/02\/23\/378866.aspx\">\na dialog box without underlying support<\/a>,\nwhich leads to unhappiness.\n<\/p>\n<p>\nIf there is some sort of message processing you want to happen\nregardless of which message loop is control,\nthen you can&#8217;t put it in your custom message loop because\n(tautologically)\nyour custom message loop is not in control\nwhen it is not running.\nBut message loops will call\n<code>Dispatch&shy;Message<\/code>,\nand that will deliver the message to your window procedure.\n(Of course, the converse also applies:\nIf you <i>want<\/i> the behavior to be suspended when a modal operation\nis in progress,\nyou can put it in your message loop.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This question was inspired by an actual customer question, but I changed a lot of it around to make for a more interesting story. (Trust me, the original story was even more boring.) A customer&#8217;s background thread posted a message to the main UI thread to signal something (details not important). They found that the [&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-3313","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>This question was inspired by an actual customer question, but I changed a lot of it around to make for a more interesting story. (Trust me, the original story was even more boring.) A customer&#8217;s background thread posted a message to the main UI thread to signal something (details not important). They found that the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3313","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=3313"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3313\/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=3313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=3313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=3313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}