How to wait on WaitHandle without message pumping? How to wait on WaitHandle without message pumping? multithreading multithreading

How to wait on WaitHandle without message pumping?


WaitForSingleObject or WaitForMultipleObjects are non-pumping waits; use p/invoke.

[DllImport("kernel32", SetLastError=true, ExactSpelling=true)]public static extern Int32 WaitForSingleObject(SafeWaitHandle handle, Int32 milliseconds);

-Oisin


Apparently this was a change introduced in Microsoft Vista: CoWaitForMultipleHandles now dispatches WM_PAINT messages.

Vista bug report.

A workaround is to use Microsoft's Application Compatibility Toolkit to set the DisableNewWMPAINTDispatchInOLE flag for your application.


If you're in a WPF app where the Dispatcher is the one running the message pumps during managed waits, the simplest way to disable the message pump during your wait is via Dispatcher.DisableProcessing:

// The Dispose() method is called at the end of the using statement. // Calling Dispose on the DispatcherProcessingDisabled structure,  // which is returned from the call to DisableProcessing, will // re-enable Dispatcher processing. using (Dispatcher.CurrentDispatcher.DisableProcessing()){    // Do work while the dispatcher processing is disabled.    Thread.Sleep(2000);}