Condividi tramite


WaitHandle.WaitAny Method (array<WaitHandle[], TimeSpan)

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, using a TimeSpan to specify the time interval.

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

Syntax

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

Parameters

  • timeout
    Type: System.TimeSpan
    A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.

Return Value

Type: System.Int32
The array index of the object that satisfied the wait, or WaitTimeout if no object satisfied the wait and a time interval equivalent to timeout has passed.

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.

ArgumentOutOfRangeException

timeout is a negative number other than -1 milliseconds, which represents an infinite time-out.

-or-

timeout is greater than Int32.MaxValue.

ArgumentException

waitHandles is an array with no elements.

Remarks

If timeout is zero, the method does not block. It tests the state of the wait handles and returns immediately.

This method returns when the wait terminates, either when any of the handles are signaled or when a time-out occurs. 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.

The maximum value for timeout is Int32.MaxValue.

Examples

The following example shows how to use this overload of the WaitAny method to report progress while waiting for multiple threads to finish. Each time the WaitAny method times out, the thread that was waiting reports progress to the user interface thread.

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

' Wait for ANY subtask to complete, and show progress.

' 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

While index = WaitHandle.WaitTimeout

   ' Wait for any WaitHandle to be signaled. Use a timeout of 250 milliseconds 
   ' to send progress reports. If a timeout occurs, WaitTimeout is returned;
   ' if a WaitHandle signals, the array index of the WaitHandle is returned.
   '
   index = WaitHandle.WaitAny(waitHandles, New TimeSpan(0, 0, 0, 0, 250))
   worker.ReportProgress(1)

End While
// Wait for ANY subtask to complete, and show progress.

// 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;

while (index == WaitHandle.WaitTimeout)
{
   // Wait for any WaitHandle to be signaled. Use a timeout of 250 milliseconds 
   // to send progress reports. If a timeout occurs, WaitTimeout is returned;
   // if a WaitHandle signals, the array index of the WaitHandle is returned.
   //
   index = WaitHandle.WaitAny(waitHandles, new TimeSpan(0, 0, 0, 0, 250));
   worker.ReportProgress(1);
}

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.