I think your method should work correctly as written.
I believe WaitHandle.WaitAny () uses the Windows API function WaitForMultipleObjects () , the documentation that says:
Modification occurs only for the object or objects whose alarm state caused a return of the function.
If true, that means your code should work.
I wrote a test program. It creates an AutoResetEvents download and sets half of them before calling CancelableWaitAll (). Then it starts a thread that waits 5 seconds before installing the other half of AutoResetEvents. Immediately after starting this thread, the main thread calls CancelableWaitAll ().
If WaitAny () actually reset any of the autosave events other than whose index was returned, CancelableWaitAll () will never return.
Since it returns (after 5 seconds, of course), I claim that your code works with AutoResetEvents:
using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace Demo { public static class Program { private static void Main(string[] args) { AutoResetEvent[] events = new AutoResetEvent[32]; for (int i = 0; i < events.Length; ++i) { events[i] = new AutoResetEvent(false); }
Unfortunately, I cannot prove that WaitHandle.WaitAll () uses WaitForMultipleObjects (). However, if this is not the case, you can call it yourself using WaitHandle.SafeWaitHandle to access the OS event windows and use P / Invoke to call WaitForMultipleObjects ().
source share