WaitHandle.WaitAny Method (array<WaitHandle[])

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function WaitAny ( _
    waitHandles As WaitHandle() _
) As Integer
public static int WaitAny(
    WaitHandle[] waitHandles
)

Parameters

Return Value

Type: System.Int32
The array index of the object that satisfied the wait.

Exceptions

Exception Condition
ArgumentNullException

The waitHandles parameter is nulla null reference (Nothing in Visual Basic).

-or-

One or more of the objects in the waitHandles array is nulla null reference (Nothing in Visual Basic).

NotSupportedException

The number of objects in waitHandles is greater than the system permits.

ArgumentException

waitHandles is an array with no elements.

Remarks

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 WaitAny(array<WaitHandle[], Int32) method overload and specifying -1 (or Timeout.Infinite) for millisecondsTimeout.

Examples

The following example demonstrates how to call the WaitAny method to wait for the first of several threads to finish.

This code is part of a larger example provided for the WaitAll(array<WaitHandle[]) method overload.

' Wait for ANY subtask to complete.

' Create an array of ManualResetEvent wait handles. Each subtask will
' signal its ManualResetEvent when it is finished.
Dim waitHandles() As WaitHandle = finished.ToArray()
Dim index As Integer = WaitHandle.WaitTimeout

index = WaitHandle.WaitAny(waitHandles)
// Wait for ANY subtask to complete.

// Create an array of ManualResetEvent wait handles. Each subtask will
// signal its ManualResetEvent when it is finished.
WaitHandle[] waitHandles = finished.ToArray();
int index = WaitHandle.WaitTimeout;

index = WaitHandle.WaitAny(waitHandles);

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.