Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Waits for any of the provided Task objects to complete execution.
WaitAny(Task[], TimeSpan) |
Waits for any of the provided Task objects to complete execution within a specified time interval. |
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[], 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. |
Waits for any of the provided Task objects to complete execution within a specified time interval.
public:
static int WaitAny(cli::array <System::Threading::Tasks::Task ^> ^ tasks, TimeSpan timeout);
public static int WaitAny(System.Threading.Tasks.Task[] tasks, TimeSpan timeout);
static member WaitAny : System.Threading.Tasks.Task[] * TimeSpan -> int
Public Shared Function WaitAny (tasks As Task(), timeout As TimeSpan) As Integer
A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.
The index of the completed task in the tasks
array argument, or -1 if the timeout occurred.
The Task has been disposed.
The tasks
argument is null
.
The TotalMilliseconds property of the timeout
argument is a negative number other than -1, which represents an infinite time-out.
-or-
The TotalMilliseconds property of the timeout
argument is greater than Int32.MaxValue.
The tasks
argument contains a null element.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
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(cli::array <System::Threading::Tasks::Task ^> ^ tasks, int millisecondsTimeout, System::Threading::CancellationToken cancellationToken);
public static int WaitAny(System.Threading.Tasks.Task[] tasks, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
static member WaitAny : System.Threading.Tasks.Task[] * int * System.Threading.CancellationToken -> int
Public Shared Function WaitAny (tasks As Task(), millisecondsTimeout As Integer, cancellationToken As CancellationToken) As Integer
The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.
A CancellationToken to observe while waiting for a task to complete.
The index of the completed task in the tasks
array argument, or -1 if the timeout occurred.
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.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Waits for any of the provided Task objects to complete execution within a specified number of milliseconds.
public:
static int WaitAny(cli::array <System::Threading::Tasks::Task ^> ^ tasks, int millisecondsTimeout);
public static int WaitAny(System.Threading.Tasks.Task[] tasks, int millisecondsTimeout);
static member WaitAny : System.Threading.Tasks.Task[] * int -> int
Public Shared Function WaitAny (tasks As Task(), millisecondsTimeout As Integer) As Integer
The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.
The index of the completed task in the tasks
array argument, or -1 if the timeout occurred.
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.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Waits for any of the provided Task objects to complete execution unless the wait is cancelled.
public:
static int WaitAny(cli::array <System::Threading::Tasks::Task ^> ^ tasks, System::Threading::CancellationToken cancellationToken);
public static int WaitAny(System.Threading.Tasks.Task[] tasks, System.Threading.CancellationToken cancellationToken);
static member WaitAny : System.Threading.Tasks.Task[] * System.Threading.CancellationToken -> int
Public Shared Function WaitAny (tasks As Task(), cancellationToken As CancellationToken) As Integer
A CancellationToken to observe while waiting for a task to complete.
The index of the completed task in the tasks
array argument.
The Task has been disposed.
The tasks
argument is null
.
The tasks
argument contains a null element.
The cancellationToken
was canceled.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Waits for any of the provided Task objects to complete execution.
public:
static int WaitAny(... cli::array <System::Threading::Tasks::Task ^> ^ tasks);
public static int WaitAny(params System.Threading.Tasks.Task[] tasks);
static member WaitAny : System.Threading.Tasks.Task[] -> int
Public Shared Function WaitAny (ParamArray tasks As Task()) As Integer
The index of the completed Task object in the tasks
array.
The Task has been disposed.
The tasks
argument is null
.
The tasks
argument contains a null element.
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
open System.Threading
open System.Threading.Tasks
let tasks =
[| for factor = 0 to 4 do
Task.Run(fun () -> Thread.Sleep(factor * 250 + 50)) |]
let index = Task.WaitAny tasks
printfn $"Wait ended because task #{tasks[index].Id} completed."
printfn "\nCurrent Status of Tasks:"
for t in tasks do
printfn $" Task {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
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in