Task.WaitAny Method

Definition

Waits for any of the provided Task objects to complete execution.

Overloads

WaitAny(Task[], Int32, CancellationToken)

Waits for any of the provided Task objects to complete execution within a specified number of milliseconds or until a cancellation token is cancelled.

WaitAny(Task[], TimeSpan)

Waits for any of the provided Task objects to complete execution within a specified time interval.

WaitAny(Task[], Int32)

Waits for any of the provided Task objects to complete execution within a specified number of milliseconds.

WaitAny(Task[], CancellationToken)

Waits for any of the provided Task objects to complete execution unless the wait is cancelled.

WaitAny(Task[])

Waits for any of the provided Task objects to complete execution.

WaitAny(Task[], Int32, CancellationToken)

Waits for any of the provided Task objects to complete execution within a specified number of milliseconds or until a cancellation token is cancelled.

public static int WaitAny (System.Threading.Tasks.Task[] tasks, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
Parameters
tasks
Task[]

An array of Task instances on which to wait.

millisecondsTimeout
Int32

The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

cancellationToken
CancellationToken

A CancellationToken to observe while waiting for a task to complete.

Returns

The index of the completed task in the tasks array argument, or -1 if the timeout occurred.

Exceptions

The Task has been disposed.

The tasks argument is null.

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

The tasks argument contains a null element.

The cancellationToken was canceled.

WaitAny(Task[], TimeSpan)

Waits for any of the provided Task objects to complete execution within a specified time interval.

public static int WaitAny (System.Threading.Tasks.Task[] tasks, TimeSpan timeout);
Parameters
tasks
Task[]

An array of Task instances on which to wait.

timeout
TimeSpan

A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.

Returns

The index of the completed task in the tasks array argument, or -1 if the timeout occurred.

Exceptions

The Task has been disposed.

The tasks argument is null.

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

-or-

timeout is greater than MaxValue.

The tasks argument contains a null element.

WaitAny(Task[], Int32)

Waits for any of the provided Task objects to complete execution within a specified number of milliseconds.

public static int WaitAny (System.Threading.Tasks.Task[] tasks, int millisecondsTimeout);
Parameters
tasks
Task[]

An array of Task instances on which to wait.

millisecondsTimeout
Int32

The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

Returns

The index of the completed task in the tasks array argument, or -1 if the timeout occurred.

Exceptions

The Task has been disposed.

The tasks argument is null.

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

The tasks argument contains a null element.

WaitAny(Task[], CancellationToken)

Waits for any of the provided Task objects to complete execution unless the wait is cancelled.

public static int WaitAny (System.Threading.Tasks.Task[] tasks, System.Threading.CancellationToken cancellationToken);
Parameters
tasks
Task[]

An array of Task instances on which to wait.

cancellationToken
CancellationToken

A CancellationToken to observe while waiting for a task to complete.

Returns

The index of the completed task in the tasks array argument.

Exceptions

The Task has been disposed.

The tasks argument is null.

The tasks argument contains a null element.

The cancellationToken was canceled.

WaitAny(Task[])

Waits for any of the provided Task objects to complete execution.

public static int WaitAny (params System.Threading.Tasks.Task[] tasks);
Parameters
tasks
Task[]

An array of Task instances on which to wait.

Returns

The index of the completed Task object in the tasks array.

Exceptions

The Task has been disposed.

The tasks argument is null.

The tasks argument contains a null element.

Examples

The following example launches five tasks, each of which sleeps for a minimum of 50 milliseconds or a maximum of 1,050 milliseconds. The WaitAny method then waits for any of the tasks to complete. The example displays the task ID of the task that ended the wait, as well as the current status of all the tasks.

using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      Task[] tasks = new Task[5];
      for (int ctr = 0; ctr <= 4; ctr++) {
         int factor = ctr;
         tasks[ctr] = Task.Run(() => Thread.Sleep(factor * 250 + 50));
      }
      int index = Task.WaitAny(tasks);
      Console.WriteLine("Wait ended because task #{0} completed.",
                        tasks[index].Id);
      Console.WriteLine("\nCurrent Status of Tasks:");
      foreach (var t in tasks)
         Console.WriteLine("   Task {0}: {1}", t.Id, t.Status);
   }
}
// The example displays output like the following:
//       Wait ended because task #1 completed.
//
//       Current Status of Tasks:
//          Task 1: RanToCompletion
//          Task 2: Running
//          Task 3: Running
//          Task 4: Running
//          Task 5: Running
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim tasks(4) As Task
      For ctr As Integer = 0 To 4
         Dim factor As Integer = ctr
         tasks(ctr) = Task.Run(Sub() Thread.Sleep(factor * 250 + 50))
      Next
      Dim index As Integer = Task.WaitAny(tasks)

      Console.WriteLine("Wait ended because task #{0} completed.",
                        tasks(index).Id)
      Console.WriteLine()
      Console.WriteLine("Current Status of Tasks:")
      For Each t In tasks
         Console.WriteLine("   Task {0}: {1}", t.Id, t.Status)
      Next
   End Sub
End Module
' The example displays output like the following:
'       Wait ended because task #1 completed.
'
'       Current Status of Tasks:
'          Task 1: RanToCompletion
'          Task 2: Running
'          Task 3: Running
'          Task 4: Running
'          Task 5: Running

Applies to