WaitHandle.WaitAny to match WaitForMultipleObjects functionality

后端 未结 3 1559
情深已故
情深已故 2021-01-19 01:04

I am porting C++ API code to .NET and looking into function call WaitHandle.WaitAny as a replacement for WaitForMultipleObjects<

3条回答
  •  独厮守ぢ
    2021-01-19 01:45

    It is fine, it uses WaitForMultipleObjects() under the hood. Which you can find out with this little test program:

    using System;
    using System.Threading;
    
    class Program {
        static void Main(string[] args) {
            var waits = new WaitHandle[65];
            for (int ix = 0; ix < waits.Length; ++ix) waits[ix] = new ManualResetEvent(false);
            WaitHandle.WaitAny(waits);
        }
    }
    

    Same restriction that WaitForMultipleObjects has. The WaitMultiple() method is marked MethodImplOptions.InternalCall because it actually lives inside the CLR. Which wants to know about blocking waits in order to provide several managed threading guarantees. Like pumping a message loop on a UI thread to keep COM happy (MsgWaitForMultipleObjects), knowing when a remoting request can be suspended for the next request and knowing when a thread is in a safe state to honor abort requests.

提交回复
热议问题