Task.WhenAny Method (System.Threading.Tasks) (original) (raw)
Definition
Namespace:
Assemblies:
netstandard.dll, System.Runtime.dll
Assemblies:
mscorlib.dll, System.Threading.Tasks.dll
Assembly:
System.Threading.Tasks.dll
Assembly:
System.Runtime.dll
Assembly:
mscorlib.dll
Assembly:
netstandard.dll
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.
Creates a task that will complete when any of the supplied tasks have completed.
Overloads
WhenAny(Task, Task)
Source:
Source:
Source:
Source:
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
The first task to wait on for completion.
task2
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
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:
Source:
Source:
Source:
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)
Source:
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(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
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
array contains a null
task, or is 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(IEnumerable)
Source:
Source:
Source:
Source:
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
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(IEnumerable<Task>)
Source:
Source:
Source:
Source:
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
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>)
Source:
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(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
The type of the result returned by the tasks.
Parameters
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
array contains a null
task, or is 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(Task[])
Source:
Source:
Source:
Source:
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[]
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(Task, Task)
Source:
Source:
Source:
Source:
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
Returns
A task that represents the completion of one of the supplied tasks. The returned task's TResult
is the task that completed first.
Exceptions
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 overload of Task.WhenAny(), when the method returns the first completed task, the other tasks will continue running until completion, even if any of them completed in the Canceled
or Faulted
state. If that behavior is undesirable, you can cancel all the remaining tasks once the first task completes.