MsgWaitForMultipleObjectsEx

Posted on September 30th, 2006 by Tim.
Categories: Programming, Tim.

Truly, this function name could only exist in the Win32 API. Apparently the documentation for this function is inaccurate. According to this sample by Raymond Chen, you can call MsgWaitForMultipleObjectsEx with an empty list of handles. Presumably, the object that the function is waiting on becomes the current thread (considering how he is using the function), but there is nothing to indicate this in the MSDN documentation.

The reason I need this function is because I’m fixing an application that doesn’t implement a proper message loop. Too many programs decide to use PeekMessage instead of GetMessage so that they can continue to process messages while using 100% CPU. The problem is that this totally borks the power management. This means that on laptops, the fans will be constantly going. It also borks the thread scheduler (so that background threads get starved) and the working set (polling will keep at least several pages of memory resident even when completely unneeded).

So, for our sakes, please do not use PeekMessage unless you really need to. Remember, polling is evil.

0 comments.

Leave a comment

Comments can contain some xhtml. Names and emails are required (emails aren't displayed), url's are optional.