{"id":20183,"date":"2008-11-17T10:00:00","date_gmt":"2008-11-17T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/11\/17\/why-bother-with-registerwaitforsingleobject-when-you-have-msgwaitformultipleobjects\/"},"modified":"2008-11-17T10:00:00","modified_gmt":"2008-11-17T10:00:00","slug":"why-bother-with-registerwaitforsingleobject-when-you-have-msgwaitformultipleobjects","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20081117-00\/?p=20183","title":{"rendered":"Why bother with RegisterWaitForSingleObject when you have MsgWaitForMultipleObjects?"},"content":{"rendered":"<p>Commenter kokorozashi wonders <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/02\/22\/536920.aspx#536958\"> why you should bother with <code>RegisterWaitForSingleObject<\/code> when you have <code>MsgWaitForMultipleObjects<\/code> already<\/a>. If you want to pump messages and wait for a kernel object, then you can change all calls to <code>PeekMessage<\/code>, <code>GetMessage<\/code>, and <code>WaitMessage<\/code> to replacement functions that use <code>MsgWaitForMultipleObjects<\/code>. Isn&#8217;t that enough? Why waste an entire thread just to wait for that object?<\/p>\n<p> If you&#8217;re so clever that you can modify every call to <code>PeekMessage<\/code>, <code>GetMessage<\/code>, and <code>WaitMessage<\/code>, then more power to you. But in order to do this, you&#8217;ll have to restrict the functions you call, because all sorts of functions contain their own message loops. Do you call <code>MessageBox<\/code>? Or <code>DialogBox<\/code>? Those functions contain a modal loop. (After all, they don&#8217;t return until the user dismisses the dialog box; <i>somebody<\/i> has to be pumping messages because you&#8217;re not.) Indeed, you can&#8217;t even call <code>DefWindowProc<\/code> since that function goes into a modal loop if the user, say, grabs the caption bar and drags the window around: That drag loop happens inside <code>DefWindowProc<\/code>. <\/p>\n<p> If your thread has any sort of visible UI, this sort of extreme control of all message loops is unreasonable. You have no practical choice but to have the wait happen on some other thread and either respond to the signalled object on that thread or post a notification back to the UI thread to do the work. <\/p>\n<p> The advantage of <code>RegisterWaitForSingleObject<\/code> over creating your own thread for waiting is that the thread pool functions will combine multiple registered waits together on a single thread (by the power of <code>WaitForMultipleObjects<\/code>), so instead of costing a whole thread, it costs something closer to (but not exactly) 1\/64 of a thread. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Commenter kokorozashi wonders why you should bother with RegisterWaitForSingleObject when you have MsgWaitForMultipleObjects already. If you want to pump messages and wait for a kernel object, then you can change all calls to PeekMessage, GetMessage, and WaitMessage to replacement functions that use MsgWaitForMultipleObjects. Isn&#8217;t that enough? Why waste an entire thread just to wait for [&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-20183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Commenter kokorozashi wonders why you should bother with RegisterWaitForSingleObject when you have MsgWaitForMultipleObjects already. If you want to pump messages and wait for a kernel object, then you can change all calls to PeekMessage, GetMessage, and WaitMessage to replacement functions that use MsgWaitForMultipleObjects. Isn&#8217;t that enough? Why waste an entire thread just to wait for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/20183","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=20183"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/20183\/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=20183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=20183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=20183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}