{"id":35973,"date":"2005-04-05T09:03:46","date_gmt":"2005-04-05T09:03:46","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/04\/05\/the-dialog-manager-part-6-subtleties-in-message-loops\/"},"modified":"2005-04-05T09:03:46","modified_gmt":"2005-04-05T09:03:46","slug":"the-dialog-manager-part-6-subtleties-in-message-loops","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050405-46\/?p=35973","title":{"rendered":"The dialog manager, part 6: Subtleties in message loops"},"content":{"rendered":"<p>\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/04\/04\/405207.aspx\">\nLast time, I left you with a homework exercise<\/a>:\nFind the subtle bug in the interaction between\n<code>EndManualModalDialog<\/code> and the modal message loop.\n<\/p>\n<p>\nThe subtlety is that <code>EndManualModalDialog<\/code>\nsets some flags but does nothing to force the message loop\nto notice that the flag was actually set.  Recall that\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>\ndoes not return until a posted message arrives in the queue.\nIf incoming sent messages arrive, they are delivered to\nthe corresponding window procedure, but the <code>GetMessage<\/code>\nfunction doesn&#8217;t return.  It just keeps delivering incoming\nsent messages until a posted message finally arrives.\n<\/p>\n<p>\nThe bug, therefore, is that when you call\n<code>EndManualModalDialog<\/code>,\nit sets the flag that tells the modal message loop to stop\nrunning, but doesn&#8217;t do anything to ensure that the modal\nmessage loop will wake up to notice.  Nothing happens until\na posted message arrives, which causes <code>GetMessage<\/code>\nto return. The posted message is dispatched\nand the <code>while<\/code> loop restarted,\nat which point the code finally notices that the <code>fEnded<\/code>\nflag is set and breaks out of the modal message loop.\n<\/p>\n<p>\nThere are a few ways of fixing this problem.\nThe quick solution is to post a meaningless message.\n<\/p>\n<pre>\nvoid EndManualModalDialog(HWND hdlg, int iResult)\n{\n DIALOGSTATE *pds = reinterpret_cast&lt;DIALOGSTATE*&gt;\n     (GetWindowLongPtr(hdlg, DWLP_USER));\n if (pds) {\n  pds-&gt;iResult = iResult;\n  pds-&gt;fEnded = TRUE;\n  <font COLOR=\"blue\">PostMessage(hdlg, WM_NULL, 0, 0);<\/font>\n }\n}\n<\/pre>\n<p>\nThis will force the <code>GetMessage<\/code> to return,\nsince we made sure there is at least one posted message\nin the queue waiting to be processed.\nWe chose\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/WinUI\/WindowsUserInterface\/Windowing\/Windows\/WindowReference\/WindowMessages\/WM_NULL.asp\">\nthe <code>WM_NULL<\/code> message<\/a>\nbecause it doesn&#8217;t do anything.\nWe aren&#8217;t interested in what the message does,\njust the fact that there is a message at all.\n<\/p>\n<p>\nNext time, a different solution to the same problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time, I left you with a homework exercise: Find the subtle bug in the interaction between EndManualModalDialog and the modal message loop. The subtlety is that EndManualModalDialog sets some flags but does nothing to force the message loop to notice that the flag was actually set. Recall that the GetMessage function does not return [&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-35973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Last time, I left you with a homework exercise: Find the subtle bug in the interaction between EndManualModalDialog and the modal message loop. The subtlety is that EndManualModalDialog sets some flags but does nothing to force the message loop to notice that the flag was actually set. Recall that the GetMessage function does not return [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35973","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=35973"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35973\/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=35973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=35973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=35973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}