views:

27

answers:

1

When creating a new STA thread to host an STA COM component, it is the responsibility of that thread to pump Windows messages related to COM. From what I've been able to gather, certain built in .NET threading primitives such as lock (Monitor.Enter) will do this for you while waiting for the object to be released by another thread. Another way of making .NET pump COM messages for you that I've seen is to use .Join().

Where can I find a complete list of built in threading primitives with this behaviour? Would waiting on a WaitHandle support this? What about WaitAny(), or the new concurrent collections in .NET 4? I'm unable to find this in the documentation for any of the specific methods.

+1  A: 

This can be reverse-engineered somewhat from the SSCLI20 source, albeit that it is dated. The core CLR function that implements MsgWaitForMultipleHandles is DoAppropriateWait. From what I see, it is used by AutoResetEvent, ManualResetEvent and Semaphore but not by Mutex. One quirk is that WaitHandle.WaitAny is okay from an STA thread but WaitAll() generates an exception. Yeah, this isn't documented anywhere other then a few hints in Chris Brumme's blog. You can't assume too much from what I found, test this to be sure.

Hans Passant
Very helpful, thank you!
Freed