WaitAny Method (WaitHandle[], Int32)
Collapse the table of content
Expand the table of content

WaitHandle.WaitAny Method (WaitHandle(), Int32)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Waits for any of the elements in the specified array to receive a signal, using a 32-bit signed integer to specify the time interval.

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

'Declaration
Public Shared Function WaitAny ( _
	waitHandles As WaitHandle(), _
	millisecondsTimeout As Integer _
) As Integer

Parameters

waitHandles
Type: System.Threading.WaitHandle ()
An array that contains the objects for which the current instance will wait.
millisecondsTimeout
Type: System.Int32
The number of milliseconds to wait, or Timeout.Infinite (-1) 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 millisecondsTimeout has passed.

ExceptionCondition
ArgumentNullException

The waitHandles parameter is Nothing.

-or-

One or more of the objects in the waitHandles array is Nothing.

NotSupportedException

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

ArgumentOutOfRangeException

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

ArgumentException

waitHandles is an array with no elements.

If millisecondsTimeout 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 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 WaitHandle class.


' 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, 250)
   worker.ReportProgress(1)

End While


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2016 Microsoft