<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx</link><description>It waits only for messages that have arrived since the last time you checked.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375324</link><pubDate>Thu, 17 Feb 2005 15:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375324</guid><dc:creator>Dave</dc:creator><description>Shouldn't that be:&lt;br&gt;&lt;br&gt;while (PeekMessage ...</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375351</link><pubDate>Thu, 17 Feb 2005 16:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375351</guid><dc:creator>George</dc:creator><description>No, that was Raymond's whole point!  If you peek, then wait, it fails as Raymond pointed out.  While it is possible that you could write it as a PeekMessage loop, just writing while(PeekMessage()) isn't enough.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375355</link><pubDate>Thu, 17 Feb 2005 16:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375355</guid><dc:creator>CN</dc:creator><description>If while's used to replace the current if, that should be enough, I think...&lt;br&gt;&lt;br&gt;Raymond, in your example, is there actually any work done in the windows proc for WM_NEWRECORD? I currently don't understand why the falling behind would increase unless the counter was actually increased inside the window proc.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375360</link><pubDate>Thu, 17 Feb 2005 16:45:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375360</guid><dc:creator>Raymond Chen</dc:creator><description>I guess I wasn't explicit enough. I assumed people would have figured out that the window procedure goes something like this: &lt;br&gt;&lt;br&gt;case WM_NEWRECORD: &lt;br&gt;process the new record;&lt;br&gt;records++; &lt;br&gt;display new record count in window; &lt;br&gt;break; &lt;br&gt;case WM_GETRECORDCOUNT: &lt;br&gt;return records; </description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375378</link><pubDate>Thu, 17 Feb 2005 17:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375378</guid><dc:creator>SS</dc:creator><description>When we have a message notification, we should process all available messages and check if the event is signalled .&lt;br&gt;&lt;br&gt;   case WAIT_OBJECT_1:&lt;br&gt;      // we have message(s) - peek and dispatch &lt;br&gt;      while (PeekMessage(&amp;amp;msg, NULL, 0, 0, PM_REMOVE)) {&lt;br&gt;        if(WaitForSingleObject(h,0) == WAIT_OBJECT_0) &lt;br&gt;	  DoSomethingWith(h); // event has been signalled &lt;br&gt;		&lt;br&gt;	TranslateMessage(&amp;amp;msg);&lt;br&gt;        DispatchMessage(&amp;amp;msg);&lt;br&gt;      }&lt;br&gt;      if (SendMessage(hwndNotify, WM_GETRECORDCOUNT,&lt;br&gt;                      0, 0) &amp;gt;= cRecordsExpected) {&lt;br&gt;        return TRUE; // we got enough records&lt;br&gt;      }&lt;br&gt;      break;&lt;br&gt;</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375390</link><pubDate>Thu, 17 Feb 2005 17:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375390</guid><dc:creator>asdf</dc:creator><description>Thanks for posting this entry. It, looks like I was doing it the wrong way in my code but never noticed any ill effects. There goes 95 and NT 4.0 support.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375400</link><pubDate>Thu, 17 Feb 2005 17:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375400</guid><dc:creator>Rob Kennedy</dc:creator><description>The WaitMessage function works the same way. It only returns on NEW messages.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375478</link><pubDate>Thu, 17 Feb 2005 18:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375478</guid><dc:creator>David Wright</dc:creator><description>Can somebody post what is the correct code?  I've spent some time reading everything, but I don't want to be left guessing whether I understood it all correctly.&lt;br&gt;Thx!&lt;br&gt;&lt;br&gt;Very helpful topic, BTW.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375489</link><pubDate>Thu, 17 Feb 2005 18:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375489</guid><dc:creator>Ray Trent</dc:creator><description>Gads... this function is so florked up that several articles have appeared in Windows Developer/MSDN magazine about it (I wrote one of them, so it stuck in my mind).&lt;br&gt;&lt;br&gt;A much more fundamental danger of this function is that it can break the synchronization of the mutex if you're not careful how you use it (recursive waits in particular are problematic). &lt;br&gt;&lt;br&gt;Consider the case where you have a Lock() function that properly first calls MsgWaitForMultipleObjects, then processes the pending messages in a loop as Raymond suggests.&lt;br&gt;&lt;br&gt;Now consider two different message handlers that need to access the protected resource:&lt;br&gt;&lt;br&gt;case WM_PAINT:&lt;br&gt;  Lock();&lt;br&gt;  if (pProtected) {&lt;br&gt;    // Call some function that calls some other&lt;br&gt;    // function that does the following:&lt;br&gt;    Lock(); // This recursive Lock is broken&lt;br&gt;    pProtected-&amp;gt;ProcessPaint();&lt;br&gt;    Unlock();&lt;br&gt;  }&lt;br&gt;  Unlock();&lt;br&gt;&lt;br&gt;case WM_KILLFOCUS:&lt;br&gt;  Lock();&lt;br&gt;  if (pProtected) {&lt;br&gt;    delete pProtected;&lt;br&gt;    pProtected = NULL;&lt;br&gt;  }&lt;br&gt;  Unlock();&lt;br&gt;&lt;br&gt;&lt;br&gt;The Lock above that's labelled &amp;quot;This recursive lock is broken&amp;quot; can process a WM_KILLFOCUS message which will successfully delete the protected resource (the thread already holds the mutex, so the wait will suceed). This, of course, causes a crash in the next line.&lt;br&gt;&lt;br&gt;This all seems to be due to the fact that MsgWaitForMultipleObjects checks the message queue *first* rather than checking the state of the waited-for object first. &lt;br&gt;&lt;br&gt;So, effectively, the necessity for processing the messages with a Peek() loop has generated a little preemptive operating system of its own, and since it's in the same thread that has the locked resource, the mutex doesn't protect.&lt;br&gt;&lt;br&gt;The only solution I was able to come up with is to always put a &amp;quot;if (WaitForSingleObject(hMutex, 0) != WAIT_OBJECT_0)&amp;quot; before you try to use MsgWaitForMultipleObjects. &lt;br&gt;&lt;br&gt;It should have been implemented this way in the OS, though.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375559</link><pubDate>Thu, 17 Feb 2005 20:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375559</guid><dc:creator>Joshua Schaeffer</dc:creator><description>I would rather have had the ability to open a read-only handle to a live event object created by the message queue. It'd be way more flexible than trying to kludge MsgWaitForMultipleObjects into whatever scheme or framework I'm working with.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375618</link><pubDate>Thu, 17 Feb 2005 21:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375618</guid><dc:creator>JDM</dc:creator><description>Can someone tell me if the following would be acceptable in the message loop for a window thread, or if I am still using the MsgWaitForMultipleObjects the wrong way.  Sorry, in advance, if the code shows up incorrectly, I have never posted here before.&lt;br&gt;Thanks.&lt;br&gt;&lt;br&gt;/*	Start message loop for this thread. */&lt;br&gt;HANDLE[] hArray = { GetCurrentThread() };&lt;br&gt;while (TRUE)&lt;br&gt;{&lt;br&gt;  MSG msg;&lt;br&gt;  DWORD dwWait;&lt;br&gt;  while (PeekMessage(&amp;amp;msg,NULL,0,0,PM_REMOVE))&lt;br&gt;  {&lt;br&gt;    if (msg.message == WM_QUIT)&lt;br&gt;    {&lt;br&gt;      ExitThread(0);&lt;br&gt;      return 1;&lt;br&gt;    }&lt;br&gt;    TranslateMessage(&amp;amp;msg);&lt;br&gt;    DispatchMessage(&amp;amp;msg);&lt;br&gt;  }&lt;br&gt;  dwWait =  MsgWaitForMultipleObjectsEx (1,     hArray, INFINITE, QS_ALLINPUT, 0);&lt;br&gt;  if (dwWait == (WAIT_OBJECT_0 + 1))&lt;br&gt;  {&lt;br&gt;    continue;&lt;br&gt;  }else{&lt;br&gt;    //  Do something..&lt;br&gt;  }&lt;br&gt;}</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375642</link><pubDate>Thu, 17 Feb 2005 22:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375642</guid><dc:creator>Pavel Lebedinsky</dc:creator><description>Ray: reentrancy is a general problem in code that uses Windows message queues. This is just something you have to deal with - the system can't protect you from all possible scenarios.&lt;br&gt;&lt;br&gt;Joshua: simply exposing an event would be very inefficient if you are only interested in a certain type of messages. You'd need some mechanism to tell the system when to signal the event. And then you'd have to deal with cases where two or more threads are waiting on the same event (should this be illegal? or should each thread get its own event object?)</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375648</link><pubDate>Thu, 17 Feb 2005 22:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375648</guid><dc:creator>Pavel Lebedinsky</dc:creator><description>Ray: reentrancy is a general problem in code that uses Windows message queues. This is just something you have to deal with - the system can't protect you from all possible scenarios.&lt;br&gt;&lt;br&gt;Joshua: simply exposing an event would be very inefficient if you are only interested in a certain type of messages. You'd need some mechanism to tell the system when to signal the event. And then you'd have to deal with cases where two or more threads are waiting on the same event (should this be illegal? or should each thread get its own event object?)</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375723</link><pubDate>Fri, 18 Feb 2005 00:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375723</guid><dc:creator>Norman Diamond</dc:creator><description>Jiggling the mouse to get a response is a common occurence in the Windows 2000 Start menu and Windows XP Classic version Start menu.  It is so common that it doesn't even take thinking.  But this base note does make me wonder.  You know, click on the Start button, move up to Programs, move to the right and locate the folder containing the link you really want to click on, but that folder doesn't expand.  You have to move the mouse to hover over another folder and then move back to the one you really wanted to expand.  So does Start menu processing contain the bug described here?</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375729</link><pubDate>Fri, 18 Feb 2005 00:17:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375729</guid><dc:creator>Raymond Chen</dc:creator><description>No that's caused by a global foreground idle hook. Remember? You complained about this last year and I debugged it for you.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375748</link><pubDate>Fri, 18 Feb 2005 00:57:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375748</guid><dc:creator>Daniel Bowen</dc:creator><description>ATL includes a function &amp;quot;AtlWaitWithMessageLoop&amp;quot;.  Back a while ago on the ATL mailing lists, there was discussion of some deadlock possibilities with it (in part due to the issue you raise here), and how it might be improved.  Here's the version that I came up with and currently use.  I've often wondered if the approach I use has any possible problems. What improvements could be made to this version?  (the formatting might get messed up)&lt;br&gt;&lt;br&gt;BOOL WaitWithMessageLoop(&lt;br&gt;	HANDLE hHandleToWaitOn,&lt;br&gt;	DWORD dwInitialTimeOutMilliseconds = 0,&lt;br&gt;	DWORD dwIterateTimeOutMilliseconds = 500)&lt;br&gt;{&lt;br&gt;	DWORD dwRet=0;&lt;br&gt;	MSG msg={0};&lt;br&gt;&lt;br&gt;	dwRet = ::WaitForSingleObject(hHandleToWaitOn, dwInitialTimeOutMilliseconds);&lt;br&gt;	if(dwRet == WAIT_OBJECT_0)&lt;br&gt;	{&lt;br&gt;		// The object is already signalled.&lt;br&gt;		return TRUE;&lt;br&gt;	}&lt;br&gt;&lt;br&gt;	while(true)&lt;br&gt;	{&lt;br&gt;		// There are one or more window message available. Dispatch them.&lt;br&gt;		while(::PeekMessage(&amp;amp;msg,NULL,NULL,NULL,PM_REMOVE))&lt;br&gt;		{&lt;br&gt;			::TranslateMessage(&amp;amp;msg);&lt;br&gt;			::DispatchMessage(&amp;amp;msg);&lt;br&gt;			dwRet = ::WaitForSingleObject(hHandleToWaitOn, 0);&lt;br&gt;			if(dwRet == WAIT_OBJECT_0)&lt;br&gt;			{&lt;br&gt;				// The object is already signalled.&lt;br&gt;				return TRUE;&lt;br&gt;			}&lt;br&gt;		}&lt;br&gt;&lt;br&gt;		// Now we've dispatched all the messages in the queue.&lt;br&gt;		// Use MsgWaitForMultipleObjects to either tell us there are&lt;br&gt;		// more messages to dispatch, or that our object has been signalled.&lt;br&gt;&lt;br&gt;		dwRet = ::MsgWaitForMultipleObjects(1, &amp;amp;hHandleToWaitOn, FALSE,&lt;br&gt;				dwIterateTimeOutMilliseconds, QS_ALLINPUT);&lt;br&gt;&lt;br&gt;		if(dwRet == WAIT_OBJECT_0)&lt;br&gt;		{&lt;br&gt;			// The event was signaled.&lt;br&gt;			return TRUE;&lt;br&gt;		}&lt;br&gt;		else if(dwRet == WAIT_OBJECT_0 + 1)&lt;br&gt;		{&lt;br&gt;			// New messages have come that need to be dispatched.&lt;br&gt;			continue;&lt;br&gt;		}&lt;br&gt;		else if(dwRet == WAIT_TIMEOUT)&lt;br&gt;		{&lt;br&gt;			// We hit our time limit, continue with the loop.&lt;br&gt;			continue;&lt;br&gt;		}&lt;br&gt;		else&lt;br&gt;		{&lt;br&gt;			// Something else happened.&lt;br&gt;			return FALSE;          &lt;br&gt;		}&lt;br&gt;	}&lt;br&gt;&lt;br&gt;	return FALSE;&lt;br&gt;}&lt;br&gt;</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375783</link><pubDate>Fri, 18 Feb 2005 01:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375783</guid><dc:creator>Cheong</dc:creator><description>Ah... This is exactly one problem that I've encountered during my serial port application development. I was totally puzzled at the random lost of records. We (the development team) did tried to figure out the pattern of problem but never success, so the problem hang there until a new collegue told us about this...</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375820</link><pubDate>Fri, 18 Feb 2005 03:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375820</guid><dc:creator>CJP</dc:creator><description>What happens if a message is enqueued between the call to MsgWaitForMultipleObjects and the call to PeekMessage...&lt;br&gt;&lt;br&gt;E.G:&lt;br&gt;&lt;br&gt;&amp;lt;- (worker thread) Message 1 Added&lt;br&gt;(processing thread) MsgWaitForMultipleObjects&lt;br&gt;&amp;lt;- (worker thread) Message 2 Added&lt;br&gt;(processing thread) PeekMessage&lt;br&gt;(processing thread) Message 2 processed&lt;br&gt;&lt;br&gt;Since PeekMessage is only looking at the top message on the queue would not the first message be 'lost' since it will be considered 'old' as far as MsgWaitForMultipleObjects is concerned?&lt;br&gt;&lt;br&gt;&lt;br&gt;Of course, I could be barking up the wrong tree, I don't have much experience with windows messaging and I suck at concurrency.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375827</link><pubDate>Fri, 18 Feb 2005 03:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375827</guid><dc:creator>Norman Diamond</dc:creator><description>2/17/2005 4:17 PM Raymond Chen&lt;br&gt;&lt;br&gt;&amp;gt; No that's caused by a global foreground idle&lt;br&gt;&amp;gt; hook. Remember? You complained about this&lt;br&gt;&lt;br&gt;Sorry, I don't remember.  Thank you for pointing it out.  Perhaps I should keep notes :-)&lt;br&gt;&lt;br&gt;&amp;gt; last year and I debugged it for you. &lt;br&gt;&lt;br&gt;Thank you again.  Do you know if it will be in Windows XP SP3 and Windows 2000 SP5?  (I don't think I need to ask if you have the power to put it there, but can still ask if you know.)</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375896</link><pubDate>Fri, 18 Feb 2005 06:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375896</guid><dc:creator>Raymond Chen</dc:creator><description>It was some program you installed that was messing with foreground idle. A translation program or something as I recall. Perhaps you could complain to the vendor of that program.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#375974</link><pubDate>Fri, 18 Feb 2005 09:40:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375974</guid><dc:creator>Tom M</dc:creator><description>Don't you have to  do something like while(::PeekMessage(&amp;amp;msg,NULL,NULL,NULL,PM_REMOVE))&lt;br&gt;{&lt;br&gt;::TranslateMessage(&amp;amp;msg);&lt;br&gt;::DispatchMessage(&amp;amp;msg);&lt;br&gt;}&lt;br&gt;on WAIT_OBJECT_0 too, or else you can forget about those messages as well?&lt;br&gt;</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#376022</link><pubDate>Fri, 18 Feb 2005 12:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:376022</guid><dc:creator>Hallvard Vassbotn</dc:creator><description>&lt;br&gt;&amp;gt; The MsgWaitForMultipleObjectsEx function lets you pass the MWMO_INPUTAVAILABLE flag to indicate that it should check for previously-ignored input. &lt;br&gt;&lt;br&gt;But this flag is not supported on NT.&lt;br&gt;&lt;br&gt;&amp;gt; You call MsgWaitForMultipleObjects and include the QS_ALLPOSTMESSAGE flag&lt;br&gt;&lt;br&gt;What is the difference between &lt;br&gt;&lt;br&gt;QS_ALLPOSTMESSAGE       = $0100;&lt;br&gt;&lt;br&gt;and&lt;br&gt;&lt;br&gt;QS_POSTMESSAGE          = $0008;&lt;br&gt;&lt;br&gt;?&lt;br&gt;&lt;br&gt;The MSDN docs says the same for both:&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/msgwaitformultipleobjectsex.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/msgwaitformultipleobjectsex.asp&lt;/a&gt;&lt;br&gt;&lt;br&gt;&amp;quot;QS_POSTMESSAGE A posted message (other than those just listed) is in the queue.&amp;quot;&lt;br&gt;&lt;br&gt;&amp;quot;QS_ALLPOSTMESSAGE A posted message (other than those listed here) is in the queue&amp;quot;&lt;br&gt;&lt;br&gt;IMO, this is a really messy API, but I think we are using it correctly - we use an object oriented wrapper (in Delphi) around the raw API calls, but it really boils down to calling &lt;br&gt;&lt;br&gt;MsgWaitForMultipleObjects with the flag:&lt;br&gt;&lt;br&gt;  QS_ALLINPUT = (QS_INPUT or QS_POSTMESSAGE or QS_TIMER or QS_PAINT or QS_HOTKEY or QS_SENDMESSAGE);&lt;br&gt;&lt;br&gt;and when it returns WAIT_OBJECT_0 + FObjCount we basically return to Delphi VCL's TApplication.ProcessMessages routine that will empty the message queue.&lt;br&gt;&lt;br&gt;</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#376098</link><pubDate>Fri, 18 Feb 2005 15:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:376098</guid><dc:creator>Dave</dc:creator><description>The very bottom of the MsgWaitForMultipleObjectsEx page says: &amp;quot;The QS_ALLPOSTMESSAGE and QS_POSTMESSAGE flags differ in when they are cleared&amp;quot; and some more detail after that.&lt;br&gt;&lt;br&gt;I sure hope an answer key is forthcoming...&lt;br&gt;&lt;br&gt;</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#376518</link><pubDate>Sat, 19 Feb 2005 09:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:376518</guid><dc:creator>Alex Blekhman</dc:creator><description>In my opinion, situations that justify MsgWaitForMultipleObjects are quite rare nowadays. In the past thread were expensive and developers needed to scratch their heads how to cram in gazillion things in one thread. Today it’s just doesn’t worth it. Besides, this is annoying when you have message loops splattered here and there in the code. If I need to process some data which involves synchronization issues, then I don’t even want to think about messing it with GUI. Just stash it in worker thread, do the job, then notify GUI in some flexible and lazy way (say, with PostMessage).</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#377135</link><pubDate>Mon, 21 Feb 2005 03:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:377135</guid><dc:creator>Norman Diamond</dc:creator><description>2/17/2005 10:05 PM Raymond Chen&lt;br&gt;&lt;br&gt;&amp;gt; It was some program you installed that was&lt;br&gt;&amp;gt; messing with foreground idle.&lt;br&gt;&lt;br&gt;That is a completely unrelated issue.  The matter of the Start menu, at random times not expanding folders unless the mouse is moved to a different folder and moved back, occurs even when all applications come from Microsoft.  It is intermittently reproducible both on real PCs and virtual PCs.  Now I resume thinking that your base note here looks like a likely explanation.  The symptom really resembles the example you gave.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#377143</link><pubDate>Mon, 21 Feb 2005 03:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:377143</guid><dc:creator>Raymond Chen</dc:creator><description>Hm well I've never experienced it. But I doubt it's this problem. Menus don't mix user and kernel waits.</description></item><item><title>Modality, part 2: Code-modality vs UI-modality</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#377339</link><pubDate>Mon, 21 Feb 2005 18:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:377339</guid><dc:creator>The Old New Thing</dc:creator><description>The dichotomy breaks in the other direction, too.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#377495</link><pubDate>Mon, 21 Feb 2005 19:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:377495</guid><dc:creator>Ian Boyd</dc:creator><description>Is there a way to tell Windows to &amp;quot;from now on pretend i don't know about any messages in the queue&amp;quot; function? Basically undo the strangeness that PeekMessage does.&lt;br&gt;&lt;br&gt;Does MsgWaitForMultipleObjects also tag queued messages as &amp;quot;read&amp;quot; like PeekMessage. i.e. Once i &amp;quot;realize&amp;quot; there is a message in the queue with MsgWaitForMultipleObjects, am i then forced to process all messages in the queue?</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#377600</link><pubDate>Mon, 21 Feb 2005 21:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:377600</guid><dc:creator>daev</dc:creator><description>&amp;quot;In my opinion, situations that justify MsgWaitForMultipleObjects are quite rare nowadays. In the past thread were expensive and developers needed to scratch their heads how to cram in gazillion things in one thread.&amp;quot;&lt;br&gt;&lt;br&gt;Actually MsgWaitForMultipleObjects turns out to be very useful in multithread situations.&lt;br&gt;&lt;br&gt;It's the only way for you to add a user-defined window message that works like WM_PAINT -- a very low priority message, and there's only one of them in the queue no matter how many times you post it.&lt;br&gt;&lt;br&gt;I used a message loop based on MsgWaitForMultipleObjects when I had a program with a worker thread that did computation and wrote output text.  There was also a main UI thread which controlled the window, so the worker thread should not be allowed to change the window contents itself.&lt;br&gt;&lt;br&gt;When the worker thread (which you can imagine as printing a million digits of Pi) has some text to output, it must post a message to the UI thread to get the text displayed.  But if you used ordinary user-defined messages, you could get thousands of tiny WM_USERs clogging up the message queue.&lt;br&gt;&lt;br&gt;The solution is to have a text buffer that holds the new output.  Any time new text is added to the buffer, the worker thread posts a WM_PAINT-type message to the UI thread.  There's only one WM_PAINT at any time, and it's always handled after more critical interactive messages (like scroll-bar manipulation).  When the UI thread services the message, it outputs all the text in the buffer at once, just as a WM_PAINT handler updates the whole invalidated region of the window, regardless of how many smaller invalidations composed it.&lt;br&gt;&lt;br&gt;But since you can't define your own WM_PAINT-type message, you can get the same effect by using MsgWaitForMultipleObjects and replacing &amp;quot;post a message&amp;quot; with &amp;quot;flip on the event object.&amp;quot;  If you write the loop correctly (as described by Raymond) you get the same behavior.&lt;br&gt;&lt;br&gt;(Raymond and others:  Are there any potential gotchas to this approach?)</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#377886</link><pubDate>Tue, 22 Feb 2005 08:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:377886</guid><dc:creator>Alex Blekhman</dc:creator><description>daev wrote:&lt;br&gt;&amp;quot;Actually MsgWaitForMultipleObjects turns out to be very useful in multithread situations. &lt;br&gt;&lt;br&gt;It's the only way for you to add a user-defined window message that works like WM_PAINT -- a very low priority message, and there's only one of them in the queue no matter how many times you post it.&amp;quot;&lt;br&gt;&lt;br&gt;I agree. That's the example of MsgWaitForMultipleObjects where it's handy. However, with two reservations:&lt;br&gt;&lt;br&gt;1. You use it to &amp;quot;improve&amp;quot; regular main message loop, not to implement bastard message loop somewhere in program.&lt;br&gt;&lt;br&gt;2. While less elegant, simple WM_TIMER with polling of text buffer is much less confusing than MsgWaitForMultipleObjects. Unless you really need to update GUI ASAP, updating it 3-5 times in second will be indistinguishable from user's point of view. Consider how significantly less explanation requires WM_TIMER to maintenace programmer than esoteric gotchas of MsgWaitForMultipleObjects.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#377887</link><pubDate>Tue, 22 Feb 2005 08:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:377887</guid><dc:creator>Alex Blekhman</dc:creator><description>daev wrote:&lt;br&gt;&amp;quot;Actually MsgWaitForMultipleObjects turns out to be very useful in multithread situations. &lt;br&gt;&lt;br&gt;It's the only way for you to add a user-defined window message that works like WM_PAINT -- a very low priority message, and there's only one of them in the queue no matter how many times you post it.&amp;quot;&lt;br&gt;&lt;br&gt;I agree. That's the example of MsgWaitForMultipleObjects where it's handy. However, with two reservations:&lt;br&gt;&lt;br&gt;1. You use it to &amp;quot;improve&amp;quot; regular main message loop, not to implement bastard message loop somewhere in program.&lt;br&gt;&lt;br&gt;2. While less elegant, simple WM_TIMER with polling of text buffer is much less confusing than MsgWaitForMultipleObjects. Unless you really need to update GUI ASAP, updating it 3-5 times in second will be indistinguishable from user's point of view. Consider how significantly less explanation requires WM_TIMER to maintenace programmer than esoteric gotchas of MsgWaitForMultipleObjects.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#379794</link><pubDate>Thu, 24 Feb 2005 18:56:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:379794</guid><dc:creator>Ray Trent</dc:creator><description>&amp;gt; Ray: reentrancy is a general problem in code &lt;br&gt;&amp;gt; that uses Windows message queues. This is just &lt;br&gt;&amp;gt; something you have to deal with - the system &lt;br&gt;&amp;gt; can't protect you from all possible scenarios. &lt;br&gt;&lt;br&gt;This is, of course, true. And as I mentioned there are numerous ways you can screw yourself, by yourself, if you're not careful.&lt;br&gt;&lt;br&gt;However, this function is the only one I know of where the action is entirely implicit. I.e. you're not calling something that you can know is going to be doing a SendMessage in the context of the current thread. You, in fact, can't know what it will do at all.&lt;br&gt;&lt;br&gt;As a result, when you're talking about a mutex that's grabbed by message handlers (which is one of the main uses of this kind of function... to avoid the inevitable deadlock that will happen if you ever do anything that requires message processing in such a handler), the only safe thing to do is to always return without processing any pending messages if the mutex is already held. &lt;br&gt;&lt;br&gt;Not only does this function not do that by default (which obviates almost its entire point), there's not even an option for it... you have to code that additional check by hand yourself or you're looking at a complete maintenance nightmare. &lt;br&gt;&lt;br&gt;It's not the end of the world, but it is an extremely obscure pitfall.</description></item><item><title>re: MsgWaitForMultipleObjects and the queue state</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#385999</link><pubDate>Sat, 05 Mar 2005 23:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:385999</guid><dc:creator>Neil</dc:creator><description>&amp;gt;It's the only way for you to add a user-defined window message that works like WM_PAINT -- a very low priority message, and there's only one of them in the queue no matter how many times you post it.&lt;br&gt;&lt;br&gt;I seem to remember WM_COALESCE_FIRST … WM_COALESCE_LAST were supposed to have had similar semantics in that there's only one of each in the queue.</description></item><item><title>Pumping messages while waiting for a period of time</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#529627</link><pubDate>Fri, 10 Feb 2006 19:54:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:529627</guid><dc:creator>The Old New Thing</dc:creator><description>Putting together pieces you already know.</description></item><item><title>COM, Re-entrancy, and Message Pumping</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#743798</link><pubDate>Thu, 07 Sep 2006 07:51:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:743798</guid><dc:creator>Tim Ng</dc:creator><description>Disclaimer: I hesitated posting this because this is a topic that is extremely complicated and deep,...</description></item><item><title>When and How should we use MsgWaitForMultipleObjects? &amp;laquo; Sharing my thoughts&amp;#8230;</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#8959129</link><pubDate>Fri, 19 Sep 2008 20:23:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8959129</guid><dc:creator>When and How should we use MsgWaitForMultipleObjects? &amp;laquo; Sharing my thoughts&amp;#8230;</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://sarathc.wordpress.com/2008/09/19/when-and-how-should-we-use-msgwaitformultipleobjects/"&gt;http://sarathc.wordpress.com/2008/09/19/when-and-how-should-we-use-msgwaitformultipleobjects/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>From Zero To One   &amp;raquo; Active Sleep procedure - Aftermath</title><link>http://blogs.msdn.com/oldnewthing/archive/2005/02/17/375307.aspx#9181259</link><pubDate>Sat, 06 Dec 2008 17:17:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9181259</guid><dc:creator>From Zero To One   &amp;raquo; Active Sleep procedure - Aftermath</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.cromis.net/blog/2008/12/active-sleep-procedure-aftermath/"&gt;http://www.cromis.net/blog/2008/12/active-sleep-procedure-aftermath/&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>