{"id":35963,"date":"2005-04-06T08:56:57","date_gmt":"2005-04-06T08:56:57","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/04\/06\/the-dialog-manager-part-7-more-subtleties-in-message-loops\/"},"modified":"2005-04-06T08:56:57","modified_gmt":"2005-04-06T08:56:57","slug":"the-dialog-manager-part-7-more-subtleties-in-message-loops","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050406-57\/?p=35963","title":{"rendered":"The dialog manager, part 7: More subtleties in message loops"},"content":{"rendered":"<p>\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/04\/05\/405518.aspx\">\nLast time, we solved the problem with\nthe <code>EndManualModalDialog<\/code> function\nby posting a harmless message<\/a>.\nToday, we&#8217;re going to solve the problem in an entirely\ndifferent way.\n<\/p>\n<p>\nThe idea here is to make sure the modal message loop\nregains control, even if all that happened were incoming\nsent messages, so that it can detect that the <code>fEnded<\/code>\nflag is set and break out of the modal loop.\n<\/p>\n<p>\nInstead of changing\nthe <code>EndManualModalDialog<\/code> function,\nwe will change the modal message loop.\n<\/p>\n<pre>\nint DoModal(HWND hwnd)\n{\n DIALOGSTATE ds = { 0 };\n HWND hdlg = CreateDialogParam(g_hinst, MAKEINTRESOURCE(1),\n             hwnd, DlgProc, reinterpret_cast&lt;LPARAM&gt;(&amp;ds));\n if (!hdlg) {\n  return -1;\n }\n EnableWindow(hwnd, FALSE);\n MSG msg;\n msg.message = WM_NULL; \/\/ anything that isn't WM_QUIT\n while (<font COLOR=\"blue\">!ds.fEnded<\/font>) {\n  <font COLOR=\"blue\">if (PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE)) {\n   if (msg.message == WM_QUIT) { \/*  fixed 8am *\/\n    break;\n   } else<\/font> if (!IsDialogMessage(hdlg, &amp;msg)) {\n    TranslateMessage(&amp;msg);\n    DispatchMessage(&amp;msg);\n   } \/* fixed 10am *\/\n  <font COLOR=\"blue\">} else if (!ds.fEnded) {\n   WaitMessage();\n  }<\/font>\n }\n if (msg.message == WM_QUIT) {\n  PostQuitMessage((int)msg.wParam);\n }\n EnableWindow(hwnd, TRUE);\n DestroyWindow(hdlg);\n return ds.iResult;\n}\n<\/pre>\n<p>\nWe changed the call to <code>GetMessage<\/code>\ninto a call to\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/WinUI\/WindowsUserInterface\/Windowing\/MessagesandMessageQueues\/MessagesandMessageQueuesReference\/MessagesandMessageQueuesFunctions\/PeekMessage.asp\">\nthe <code>PeekMessage<\/code> function<\/a>,\nasking to remove the peeked message if any.\nLike <code>GetMessage<\/code>,\nthis delivers any incoming sent messages,\nthen checks if there are any posted messages in the queue.\nThe difference is that whereas <code>GetMessage<\/code>\nkeeps waiting if there are no posted message,\n<code>PeekMessage<\/code> returns and tells you that there\nwere no posted messages.\n<\/p>\n<p>\nThat&#8217;s the control we want.\nIf <code>PeekMessage<\/code> says that it couldn&#8217;t\nfind a posted message, we check our <code>fEnded<\/code>\nflag once again, in case an incoming sent message\nset the <code>fEnded<\/code> flag.\nIf not, then we call\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/messagesandmessagequeues\/messagesandmessagequeuesreference\/messagesandmessagequeuesfunctions\/waitmessage.asp\">\nthe <code>WaitMessage<\/code> function<\/a>\nto wait until there is something to do\n(either an incoming sent message or a posted message).\n<\/p>\n<p>\nExercise: If the whole point was to regain control\nafter sent messages are delivered, why isn&#8217;t there a test\nof the <code>fEnded<\/code> flag immediately after\n<code>DispatchMessage<\/code> returns?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time, we solved the problem with the EndManualModalDialog function by posting a harmless message. Today, we&#8217;re going to solve the problem in an entirely different way. The idea here is to make sure the modal message loop regains control, even if all that happened were incoming sent messages, so that it can detect that [&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-35963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Last time, we solved the problem with the EndManualModalDialog function by posting a harmless message. Today, we&#8217;re going to solve the problem in an entirely different way. The idea here is to make sure the modal message loop regains control, even if all that happened were incoming sent messages, so that it can detect that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35963","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=35963"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35963\/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=35963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=35963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=35963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}