System.Threading.WaitHandle.WaitAny Method

Waits for any of the elements in the specified array to receive a signal.

Syntax

[System.Runtime.ConstrainedExecution.ReliabilityContract(System.Runtime.ConstrainedExecution.Consistency.WillNotCorruptState, System.Runtime.ConstrainedExecution.Cer.MayFail)]
public static int WaitAny (WaitHandle[] waitHandles)

Parameters

waitHandles
A WaitHandle array containing the objects for which the current instance will wait.

Returns

The array index of the object that satisfied the wait.

Exceptions

TypeReason
ArgumentNullException waitHandles is null or one or more elements in the waitHandles array is null.
DuplicateWaitObjectException waitHandles contains elements that are duplicates.
NotSupportedExceptionThe number of objects in waitHandles is greater than the system permits.

Remarks

System.Threading.AbandonedMutexException is new in the .NET Framework version 2.0. In previous versions, the erload:System.Threading.WaitHandle.WaitAny method returns true if the wait completes because a mutex is abandoned. An abandoned mutex often indicates a serious coding error. In the case of a system-wide mutex, it might indicate that an application has been terminated abruptly (for example, by using Windows Task Manager). The exception contains information useful for debugging.

The erload:System.Threading.WaitHandle.WaitAny method throws an System.Threading.AbandonedMutexException only when the wait completes because of an abandoned mutex. If waitHandles contains a released mutex with a lower index number than the abandoned mutex, the erload:System.Threading.WaitHandle.WaitAny method completes normally and the exception is not thrown.

Note:

In versions of the .NET Framework earlier than version 2.0, if a thread exits or aborts without explicitly releasing a System.Threading.Mutex, and that Mutex is at index 0 (zero) in a WaitAny array on another thread, the index returned by WaitAny is 128 instead of 0.

This method returns when any handle is signaled. If more than one object becomes signaled during the call, the return value is the array index of the signaled object with the smallest index value of all the signaled objects. On some implementations, if more that 64 handles are passed, a NotSupportedException is thrown.

Calling this method overload is equivalent to calling the WaitHandle.WaitAny(WaitHandle[], int, bool) method overload and specifying -1 (or Timeout.Infinite) for millisecondsTimeout and true for exitContext.

Requirements

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0