Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
WaitHandle Class
WaitAny Method
 WaitAny Method (WaitHandle[])

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
WaitHandle..::.WaitAny Method (array<WaitHandle>[]()[])

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

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Shared Function WaitAny ( _
    waitHandles As WaitHandle() _
) As Integer
Visual Basic (Usage)
Dim waitHandles As WaitHandle()
Dim returnValue As Integer

returnValue = WaitHandle.WaitAny(waitHandles)
C#
public static int WaitAny(
    WaitHandle[] waitHandles
)
Visual C++
public:
static int WaitAny(
    array<WaitHandle^>^ waitHandles
)
JScript
public static function WaitAny(
    waitHandles : WaitHandle[]
) : int

Parameters

waitHandles
Type: array<System.Threading..::.WaitHandle>[]()[]
A WaitHandle array containing the objects for which the current instance will wait.

Return Value

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

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

-or-

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

NotSupportedException

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

ApplicationException

waitHandles is an array with no elements, and the .NET Framework version is 1.0 or 1.1.

AbandonedMutexException

The wait completed because a thread exited without releasing a mutex. This exception is not thrown on Windows 98 or Windows Millennium Edition.

ArgumentException

waitHandles is an array with no elements, and the .NET Framework version is 2.0 or later.

InvalidOperationException

The waitHandles array contains a transparent proxy for a WaitHandle in another application domain.

AbandonedMutexException is new in the .NET Framework version 2.0. In previous versions, the 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 WaitAny method throws an 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 WaitAny method completes normally and the exception is not thrown.

NoteNote:

In versions of the .NET Framework earlier than version 2.0, if a thread exits or aborts without explicitly releasing a 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 WaitAny(array<WaitHandle>[]()[], Int32, Boolean) method overload and specifying -1 (or Timeout..::.Infinite) for millisecondsTimeout and true for exitContext.

The following code example demonstrates calling the WaitAny method. This code example is part of a larger example provided for the WaitHandle class.

Visual Basic
<MTAThreadAttribute> _
Public Shared Sub Main() 
    ' Queue two tasks on two different threads; 
    ' wait until all tasks are completed.
    Dim dt As DateTime = DateTime.Now
    Console.WriteLine("Main thread is waiting for BOTH tasks to complete.")
    ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(0))
    ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(1))
    WaitHandle.WaitAll(waitHandles)
    ' The time shown below should match the longest task.
    Console.WriteLine("Both tasks are completed (time waited={0})", _
        (DateTime.Now - dt).TotalMilliseconds)

    ' Queue up two tasks on two different threads; 
    ' wait until any tasks are completed.
    dt = DateTime.Now
    Console.WriteLine()
    Console.WriteLine("The main thread is waiting for either task to complete.")
    ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(0))
    ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(1))
    Dim index As Integer = WaitHandle.WaitAny(waitHandles)
    ' The time shown below should match the shortest task.
    Console.WriteLine("Task {0} finished first (time waited={1}).", _
        index + 1,(DateTime.Now - dt).TotalMilliseconds)

End Sub 'Main


C#
static void Main() 
{
    // Queue up two tasks on two different threads; 
    // wait until all tasks are completed.
    DateTime dt = DateTime.Now;
    Console.WriteLine("Main thread is waiting for BOTH tasks to complete.");
    ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
    ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
    WaitHandle.WaitAll(waitHandles);
    // The time shown below should match the longest task.
    Console.WriteLine("Both tasks are completed (time waited={0})", 
        (DateTime.Now - dt).TotalMilliseconds);

    // Queue up two tasks on two different threads; 
    // wait until any tasks are completed.
    dt = DateTime.Now;
    Console.WriteLine();
    Console.WriteLine("The main thread is waiting for either task to complete.");
    ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
    ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
    int index = WaitHandle.WaitAny(waitHandles);
    // The time shown below should match the shortest task.
    Console.WriteLine("Task {0} finished first (time waited={1}).",
        index + 1, (DateTime.Now - dt).TotalMilliseconds);
}

Visual C++
int main()
{
    // Define an array with two AutoResetEvent WaitHandles.
    array<WaitHandle^>^ handles = gcnew array<WaitHandle^> {
        gcnew AutoResetEvent(false), gcnew AutoResetEvent(false)};

    // Queue up two tasks on two different threads;
    // wait until all tasks are completed.
    DateTime timeInstance = DateTime::Now;
    Console::WriteLine("Main thread is waiting for BOTH tasks to " +
        "complete.");
    ThreadPool::QueueUserWorkItem(
        gcnew WaitCallback(WaitHandleExample::DoTask), handles[0]);
    ThreadPool::QueueUserWorkItem(
        gcnew WaitCallback(WaitHandleExample::DoTask), handles[1]);
    WaitHandle::WaitAll(handles);
    // The time shown below should match the longest task.
    Console::WriteLine("Both tasks are completed (time waited={0})",
        (DateTime::Now - timeInstance).TotalMilliseconds);

    // Queue up two tasks on two different threads;
    // wait until any tasks are completed.
    timeInstance = DateTime::Now;
    Console::WriteLine();
    Console::WriteLine("The main thread is waiting for either task to " +
        "complete.");
    ThreadPool::QueueUserWorkItem(
        gcnew WaitCallback(WaitHandleExample::DoTask), handles[0]);
    ThreadPool::QueueUserWorkItem(
        gcnew WaitCallback(WaitHandleExample::DoTask), handles[1]);
    int index = WaitHandle::WaitAny(handles);
    // The time shown below should match the shortest task.
    Console::WriteLine("Task {0} finished first (time waited={1}).",
        index + 1, (DateTime::Now - timeInstance).TotalMilliseconds);
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker