{"id":36333,"date":"2005-03-01T07:00:00","date_gmt":"2005-03-01T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/03\/01\/modality-part-7-a-timed-messagebox-the-cheap-version\/"},"modified":"2005-03-01T07:00:00","modified_gmt":"2005-03-01T07:00:00","slug":"modality-part-7-a-timed-messagebox-the-cheap-version","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050301-00\/?p=36333","title":{"rendered":"Modality, part 7: A timed MessageBox, the cheap version"},"content":{"rendered":"<p>\n<a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/02\/22\/378018.aspx\">\nAs we noted at the end of part&nbsp;3<\/a>,\nnow that you know the conventions surrounding\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/WinUI\/WindowsUserInterface\/Windowing\/Windows\/WindowReference\/WindowMessages\/WM_QUIT.asp\">\nthe <code>WM_QUIT<\/code> message<\/a>\n you can put them to your advantage.\n<\/p>\n<p>\nThe more robust you want the <code>TimedMessageBox<\/code> function\nto be, the more work you need to do.\nHere&#8217;s the cheap version,\nbased on the\n<a HREF=\"http:\/\/support.microsoft.com\/?scid=181934\">\nsample in the Knowledge Base<\/a>,\nbut with some additional bug fixes.\n<\/p>\n<pre>\nstatic BOOL s_fTimedOut;\nstatic HWND s_hwndMBOwnerEnable;\nvoid CALLBACK\nCheapMsgBoxTooLateProc(HWND hWnd, UINT uiMsg, UINT_PTR idEvent, DWORD dwTime)\n{\n    s_fTimedOut = TRUE;\n    if (s_hwndMBOwnerEnable) EnableWindow(s_hwndMBOwnerEnable, TRUE);\n    PostQuitMessage(42); \/\/ value not important\n}\n\/\/ Warning! Not thread-safe! See discussion.\nint CheapTimedMessageBox(HWND hwndOwner, LPCTSTR ptszText,\n    LPCTSTR ptszCaption, UINT uType, DWORD dwTimeout)\n{\n    s_fTimedOut = FALSE;\n    s_hwndMBOwnerEnable = NULL;\n    if (hwndOwner &amp;&amp; IsWindowEnabled(hwndOwner)) {\n      s_hwndMBOwnerEnable = hwndOwner;\n    }\n    UINT idTimer = SetTimer(NULL, 0, dwTimeout, CheapMsgBoxTooLateProc);\n    int iResult = MessageBox(hwndOwner, ptszText, ptszCaption, uType);\n    if (idTimer) KillTimer(NULL, idTimer);\n    if (s_fTimedOut) {\t\t\t\/\/ We timed out\n\tMSG msg;\n\t\/\/ Eat the fake WM_QUIT message we generated\n\tPeekMessage(&amp;msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE);\n\tiResult = -1;\n    }\n    return iResult;\n}\n<\/pre>\n<p>\nThis <code>CheapTimedMessageBox<\/code> function acts just like\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/dialogboxes\/dialogboxreference\/dialogboxfunctions\/messagebox.asp\">\nthe <code>MessageBox<\/code> function<\/a>,\nexcept that if the user doesn&#8217;t respond\nwithin <code>dwTimeout<\/code> milliseconds, we return -1.\nThe limitation is that only one timed message box can be active at a time.\nIf your program is single-threaded, this is not a serious limitation,\nbut if your program is multi-threaded, this will be a problem.\n<\/p>\n<p>\nDo you see how it works?\n<\/p>\n<p>\nThe global static variable <code>s_fTimedOut<\/code>\ntells us whether we generated\na fake <code>WM_QUIT<\/code> message as a result of a timeout.\nWhen the <code>MessageBox<\/code> function returns, and we indeed timed out,\nwe use\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>\nto remove the fake <code>WM_QUIT<\/code> message from the\nqueue before returning.\n<\/p>\n<p>\nNote that we remove the <code>WM_QUIT<\/code> message only if we were the ones\nwho generated it.\nIn this way, <code>WM_QUIT<\/code> messages generated by other\nparts of the program remain in the queue for processing by the\nmain message loop.\n<\/p>\n<p>\nNote also that when we decide that the timeout has occurred,\nwe re-enable the original owner window before we cause\nthe message box to bail out of its message loop by posting a quit message.\nThose are the rules for\n<a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/02\/27\/81155.aspx\">\nthe correct order for disabling and enabling windows<\/a>.\n<\/p>\n<p>\nNote also that we used a thread timer rather than a window timer.\nThat&#8217;s because we don&#8217;t own the window being passed in and therefore\ndon&#8217;t know what timer IDs are safe to use.  Any timer ID we pick\nmight happen to collide with a timer ID being used by that window,\nresulting in erratic behavior.\n<\/p>\n<p>\nRecall that when you pass <code>NULL<\/code> as the\n<code>hwnd<\/code> parameter to\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/timers\/timerreference\/timerfunctions\/settimer.asp\">\nthe <code>SetTimer<\/code> function<\/a>\nand also pass zero as the\n<code>nIDEvent<\/code> parameter,\nthen the <code>SetTimer<\/code> function\ncreates a brand new timer, assigns it a unique ID, and returns the ID.\nMost people, when they read that part of the specification for\n<code>SetTimer<\/code>,\nscratch their heads and ask themselves, &#8220;Why would\nanybody want to use this?&#8221;\n<\/p>\n<p>\nWell, this is one scenario where this is exactly what you want.\n<\/p>\n<p>\nNext comes the job of making the function a tad more robust.\nBut before we do that, we&#8217;ll need two quick sidebars.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As we noted at the end of part&nbsp;3, now that you know the conventions surrounding the WM_QUIT message you can put them to your advantage. The more robust you want the TimedMessageBox function to be, the more work you need to do. Here&#8217;s the cheap version, based on the sample in the Knowledge Base, but [&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,143],"class_list":["post-36333","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code","tag-modality"],"acf":[],"blog_post_summary":"<p>As we noted at the end of part&nbsp;3, now that you know the conventions surrounding the WM_QUIT message you can put them to your advantage. The more robust you want the TimedMessageBox function to be, the more work you need to do. Here&#8217;s the cheap version, based on the sample in the Knowledge Base, but [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/36333","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=36333"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/36333\/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=36333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=36333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=36333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}