Edit

Share via


Task.WhenAny Method

Definition

Creates a task that will complete when any of the supplied tasks have completed.

Overloads

WhenAny(Task, Task)

Creates a task that will complete when either of the supplied tasks have completed.

WhenAny(Task[])

Creates a task that will complete when any of the supplied tasks have completed.

WhenAny(ReadOnlySpan<Task>)
WhenAny(IEnumerable<Task>)

Creates a task that will complete when any of the supplied tasks have completed.

WhenAny<TResult>(IEnumerable<Task<TResult>>)

Creates a task that will complete when any of the supplied tasks have completed.

WhenAny<TResult>(ReadOnlySpan<Task<TResult>>)
WhenAny<TResult>(Task<TResult>[])

Creates a task that will complete when any of the supplied tasks have completed.

WhenAny<TResult>(Task<TResult>, Task<TResult>)

Creates a task that will complete when either of the supplied tasks have completed.

WhenAny(Task, Task)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

Creates a task that will complete when either of the supplied tasks have completed.

public:
 static System::Threading::Tasks::Task<System::Threading::Tasks::Task ^> ^ WhenAny(System::Threading::Tasks::Task ^ task1, System::Threading::Tasks::Task ^ task2);
public static System.Threading.Tasks.Task<System.Threading.Tasks.Task> WhenAny (System.Threading.Tasks.Task task1, System.Threading.Tasks.Task task2);
static member WhenAny : System.Threading.Tasks.Task * System.Threading.Tasks.Task -> System.Threading.Tasks.Task<System.Threading.Tasks.Task>
Public Shared Function WhenAny (task1 As Task, task2 As Task) As Task(Of Task)

Parameters

task1
Task

The first task to wait on for completion.

task2
Task

The second task to wait on for completion.

Returns

A new task that represents the completion of one of the supplied tasks. Its Result is the task that completed first.

Exceptions

task1 or task2 was null.

Remarks

The returned task will complete when any of the supplied tasks has completed. The returned task will always end in the RanToCompletion state with its Result set to the first task to complete. The result value is true even if the first task to complete ended in the Canceled or Faulted state.

Applies to

WhenAny(Task[])

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

Creates a task that will complete when any of the supplied tasks have completed.

public:
 static System::Threading::Tasks::Task<System::Threading::Tasks::Task ^> ^ WhenAny(... cli::array <System::Threading::Tasks::Task ^> ^ tasks);
public static System.Threading.Tasks.Task<System.Threading.Tasks.Task> WhenAny (params System.Threading.Tasks.Task[] tasks);
static member WhenAny : System.Threading.Tasks.Task[] -> System.Threading.Tasks.Task<System.Threading.Tasks.Task>
Public Shared Function WhenAny (ParamArray tasks As Task()) As Task(Of Task)

Parameters

tasks
Task[]

The tasks to wait on for completion.

Returns

A task that represents the completion of one of the supplied tasks. The return task's Result is the task that completed.

Exceptions

The tasks argument was null.

The tasks array contained a null task, or was empty.

Remarks

The returned task will complete when any of the supplied tasks has completed. The returned task will always end in the RanToCompletion state with its Result set to the first task to complete. This is true even if the first task to complete ended in the Canceled or Faulted state.

Applies to

WhenAny(ReadOnlySpan<Task>)

public:
 static System::Threading::Tasks::Task<System::Threading::Tasks::Task ^> ^ WhenAny(ReadOnlySpan<System::Threading::Tasks::Task ^> tasks);
public static System.Threading.Tasks.Task<System.Threading.Tasks.Task> WhenAny (scoped ReadOnlySpan<System.Threading.Tasks.Task> tasks);
static member WhenAny : ReadOnlySpan<System.Threading.Tasks.Task> -> System.Threading.Tasks.Task<System.Threading.Tasks.Task>
Public Shared Function WhenAny (tasks As ReadOnlySpan(Of Task)) As Task(Of Task)

Parameters

Returns

Applies to

WhenAny(IEnumerable<Task>)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

Creates a task that will complete when any of the supplied tasks have completed.

public:
 static System::Threading::Tasks::Task<System::Threading::Tasks::Task ^> ^ WhenAny(System::Collections::Generic::IEnumerable<System::Threading::Tasks::Task ^> ^ tasks);
public static System.Threading.Tasks.Task<System.Threading.Tasks.Task> WhenAny (System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task> tasks);
static member WhenAny : seq<System.Threading.Tasks.Task> -> System.Threading.Tasks.Task<System.Threading.Tasks.Task>
Public Shared Function WhenAny (tasks As IEnumerable(Of Task)) As Task(Of Task)

Parameters

tasks
IEnumerable<Task>

The tasks to wait on for completion.

Returns

A task that represents the completion of one of the supplied tasks. The return task's Result is the task that completed.

Exceptions

The tasks argument was null.

The tasks array contained a null task, or was empty.

Remarks

The returned task will complete when any of the supplied tasks has completed. The returned task will always end in the RanToCompletion state with its Result set to the first task to complete. This is true even if the first task to complete ended in the Canceled or Faulted state.

Applies to

WhenAny<TResult>(IEnumerable<Task<TResult>>)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

Creates a task that will complete when any of the supplied tasks have completed.

public:
generic <typename TResult>
 static System::Threading::Tasks::Task<System::Threading::Tasks::Task<TResult> ^> ^ WhenAny(System::Collections::Generic::IEnumerable<System::Threading::Tasks::Task<TResult> ^> ^ tasks);
public static System.Threading.Tasks.Task<System.Threading.Tasks.Task<TResult>> WhenAny<TResult> (System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task<TResult>> tasks);
static member WhenAny : seq<System.Threading.Tasks.Task<'Result>> -> System.Threading.Tasks.Task<System.Threading.Tasks.Task<'Result>>
Public Shared Function WhenAny(Of TResult) (tasks As IEnumerable(Of Task(Of TResult))) As Task(Of Task(Of TResult))

Type Parameters

TResult

The type of the completed task.

Parameters

tasks
IEnumerable<Task<TResult>>

The tasks to wait on for completion.

Returns

Task<Task<TResult>>

A task that represents the completion of one of the supplied tasks. The return task's Result is the task that completed.

Exceptions

The tasks argument was null.

The tasks array contained a null task, or was empty.

Remarks

The returned task will complete when any of the supplied tasks has completed. The returned task will always end in the RanToCompletion state with its Result set to the first task to complete. This is true even if the first task to complete ended in the Canceled or Faulted state.

Applies to

WhenAny<TResult>(ReadOnlySpan<Task<TResult>>)

public:
generic <typename TResult>
 static System::Threading::Tasks::Task<System::Threading::Tasks::Task<TResult> ^> ^ WhenAny(ReadOnlySpan<System::Threading::Tasks::Task<TResult> ^> tasks);
public static System.Threading.Tasks.Task<System.Threading.Tasks.Task<TResult>> WhenAny<TResult> (scoped ReadOnlySpan<System.Threading.Tasks.Task<TResult>> tasks);
static member WhenAny : ReadOnlySpan<System.Threading.Tasks.Task<'Result>> -> System.Threading.Tasks.Task<System.Threading.Tasks.Task<'Result>>
Public Shared Function WhenAny(Of TResult) (tasks As ReadOnlySpan(Of Task(Of TResult))) As Task(Of Task(Of TResult))

Type Parameters

TResult

Parameters

tasks
ReadOnlySpan<Task<TResult>>

Returns

Task<Task<TResult>>

Applies to

WhenAny<TResult>(Task<TResult>[])

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

Creates a task that will complete when any of the supplied tasks have completed.

public:
generic <typename TResult>
 static System::Threading::Tasks::Task<System::Threading::Tasks::Task<TResult> ^> ^ WhenAny(... cli::array <System::Threading::Tasks::Task<TResult> ^> ^ tasks);
public static System.Threading.Tasks.Task<System.Threading.Tasks.Task<TResult>> WhenAny<TResult> (params System.Threading.Tasks.Task<TResult>[] tasks);
static member WhenAny : System.Threading.Tasks.Task<'Result>[] -> System.Threading.Tasks.Task<System.Threading.Tasks.Task<'Result>>
Public Shared Function WhenAny(Of TResult) (ParamArray tasks As Task(Of TResult)()) As Task(Of Task(Of TResult))

Type Parameters

TResult

The type of the completed task.

Parameters

tasks
Task<TResult>[]

The tasks to wait on for completion.

Returns

Task<Task<TResult>>

A task that represents the completion of one of the supplied tasks. The return task's Result is the task that completed.

Exceptions

The tasks argument was null.

The tasks array contained a null task, or was empty.

Remarks

The returned task will complete when any of the supplied tasks has completed. The returned task will always end in the RanToCompletion state with its Result set to the first task to complete. This is true even if the first task to complete ended in the Canceled or Faulted state.

Applies to

WhenAny<TResult>(Task<TResult>, Task<TResult>)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

Creates a task that will complete when either of the supplied tasks have completed.

public:
generic <typename TResult>
 static System::Threading::Tasks::Task<System::Threading::Tasks::Task<TResult> ^> ^ WhenAny(System::Threading::Tasks::Task<TResult> ^ task1, System::Threading::Tasks::Task<TResult> ^ task2);
public static System.Threading.Tasks.Task<System.Threading.Tasks.Task<TResult>> WhenAny<TResult> (System.Threading.Tasks.Task<TResult> task1, System.Threading.Tasks.Task<TResult> task2);
static member WhenAny : System.Threading.Tasks.Task<'Result> * System.Threading.Tasks.Task<'Result> -> System.Threading.Tasks.Task<System.Threading.Tasks.Task<'Result>>
Public Shared Function WhenAny(Of TResult) (task1 As Task(Of TResult), task2 As Task(Of TResult)) As Task(Of Task(Of TResult))

Type Parameters

TResult

The type of the result of the returned task.

Parameters

task1
Task<TResult>

The first task to wait on for completion.

task2
Task<TResult>

The second task to wait on for completion.

Returns

Task<Task<TResult>>

A task that represents the completion of one of the supplied tasks. The returned task's TResult is the task that completed first.

Exceptions

task1 or task2 was null.

Remarks

The returned task will complete when any of the supplied tasks has completed. The returned task will always end in the RanToCompletion state with its Result set to the first task to complete. The result value is true even if the first task to complete ended in the Canceled or Faulted state.

Tip

In every overloaded version of Task.WhenAny() when this method returns the first completed task, the other tasks will continue running until completion, even any of them completed in the Canceled or Faulted state. So, if that behavior is not desired you may want to cancel all the remaining tasks once the first task complete.

Applies to