{"id":32513,"date":"2006-01-26T10:00:00","date_gmt":"2006-01-26T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2006\/01\/26\/pumping-messages-while-waiting-for-a-period-of-time\/"},"modified":"2006-01-26T10:00:00","modified_gmt":"2006-01-26T10:00:00","slug":"pumping-messages-while-waiting-for-a-period-of-time","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20060126-00\/?p=32513\/","title":{"rendered":"Pumping messages while waiting for a period of time"},"content":{"rendered":"<p>\nWe can use the <code>MsgWaitForMultipleObjects<\/code> function\n(or its superset <code>MsgWaitForMultipleObjectsEx<\/code>)\nto carry out a non-polling &#8220;sleep while processing messages&#8221;.\n<\/p>\n<pre>\n#define MSGF_SLEEPMSG 0x5300\nBOOL SleepMsg(DWORD dwTimeout)\n{\n DWORD dwStart = GetTickCount();\n DWORD dwElapsed;\n while ((dwElapsed = <a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/05\/31\/423407.aspx\">GetTickCount() - dwStart<\/a>) &lt; dwTimeout) {\n  DWORD dwStatus = MsgWaitForMultipleObjectsEx(0, NULL,\n                    dwTimeout - dwElapsed, QS_ALLINPUT,\n                    MWFMO_WAITANY | <a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/02\/17\/375307.aspx\">MWMO_INPUTAVAILABLE<\/a>);\n  if (dwStatus == WAIT_OBJECT_0) {\n   MSG msg;\n   while (PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE)) {\n    if (<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/02\/22\/378018.aspx\">msg.message == WM_QUIT<\/a>) {\n     PostQuitMessage(<a HREF=\"http:\/\/windowssdk.msdn.microsoft.com\/library\/en-us\/Win64\/win64\/rules_for_using_pointers.asp\">(int)<\/a>msg.wParam);\n     return FALSE; \/\/ abandoned due to WM_QUIT\n    }\n    if (!<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/04\/28\/412574.aspx\">CallMsgFilter(&amp;msg, MSGF_SLEEPMSG)<\/a>) {\n     TranslateMessage(&amp;msg);\n     DispatchMessage(&amp;msg);\n    }\n   }\n  }\n }\n return TRUE; \/\/ timed out\n}\n<\/pre>\n<p>\nThis function pumps messages for up to\n<code>dwTimeout<\/code> milliseconds.\nThe kernel of the idea is merely to use the\n<code>MsgWaitForMultipleObjects\/Ex<\/code> function as a\nsurrogate for <code>WaitMessageTimeout<\/code>,\npumping messages until the cumulative timeout has been reached.\nThere are a lot of small details to pay heed to, however.\nI&#8217;ve linked them to earlier postings that discuss the specific\nissues, if you need a refresher.\nThe <code>CallMsgFilter<\/code> you might find gratuitous,\nbut you&#8217;ll change your mind when you realize that users might\npress a keyboard accelerator while you&#8217;re sleeping, and you\npresumably want it to go through somebody&#8217;s\n<code>TranslateAccelerator<\/code>.\nThe message filter lets you hook into the modal loop and do\nyour accelerator translation.\n<\/p>\n<p>\nExtending this function to &#8220;wait on a set of handles up to\na specified amount of time, while pumping messages&#8221; is left\nas an exercise.\n(You can do it without changing very many lines of code.)\n<\/p>\n<p>\n[Call the right function. -2pm]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We can use the MsgWaitForMultipleObjects function (or its superset MsgWaitForMultipleObjectsEx) to carry out a non-polling &#8220;sleep while processing messages&#8221;. #define MSGF_SLEEPMSG 0x5300 BOOL SleepMsg(DWORD dwTimeout) { DWORD dwStart = GetTickCount(); DWORD dwElapsed; while ((dwElapsed = GetTickCount() &#8211; dwStart) &lt; dwTimeout) { DWORD dwStatus = MsgWaitForMultipleObjectsEx(0, NULL, dwTimeout &#8211; dwElapsed, QS_ALLINPUT, MWFMO_WAITANY | MWMO_INPUTAVAILABLE); if (dwStatus [&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-32513","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>We can use the MsgWaitForMultipleObjects function (or its superset MsgWaitForMultipleObjectsEx) to carry out a non-polling &#8220;sleep while processing messages&#8221;. #define MSGF_SLEEPMSG 0x5300 BOOL SleepMsg(DWORD dwTimeout) { DWORD dwStart = GetTickCount(); DWORD dwElapsed; while ((dwElapsed = GetTickCount() &#8211; dwStart) &lt; dwTimeout) { DWORD dwStatus = MsgWaitForMultipleObjectsEx(0, NULL, dwTimeout &#8211; dwElapsed, QS_ALLINPUT, MWFMO_WAITANY | MWMO_INPUTAVAILABLE); if (dwStatus [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/32513","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=32513"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/32513\/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=32513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=32513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=32513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}