Task.WhenAny 方法

定义

任何提供的任务已完成时,创建将完成的任务。Creates a task that will complete when any of the supplied tasks have completed.

重载

WhenAny(IEnumerable<Task>)

任何提供的任务已完成时,创建将完成的任务。Creates a task that will complete when any of the supplied tasks have completed.

WhenAny(Task[])

任何提供的任务已完成时,创建将完成的任务。Creates a task that will complete when any of the supplied tasks have completed.

WhenAny(Task, Task)

提供的任一任务完成时,创建将完成的任务。Creates a task that will complete when either 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>(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(IEnumerable<Task>)

任何提供的任务已完成时,创建将完成的任务。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)

参数

tasks
IEnumerable<Task>

等待完成的任务。The tasks to wait on for completion.

返回

Task<Task>

表示提供的任务之一已完成的任务。A task that represents the completion of one of the supplied tasks. 返回任务的结果是完成的任务。The return task's Result is the task that completed.

例外

tasks 参数为 nullThe tasks argument was null.

tasks 数组包含了 null 任务,或为空。The tasks array contained a null task, or was empty.

注解

当任何提供的任务完成时,将完成返回的任务。The returned task will complete when any of the supplied tasks has completed. 返回的任务将始终以状态结束, RanToCompletion 并将其结果集设置为要完成的第一个任务。The returned task will always end in the RanToCompletion state with its Result set to the first task to complete. 即使完成的第一个任务以或状态结束,也是 Canceled 如此 FaultedThis is true even if the first task to complete ended in the Canceled or Faulted state.

适用于

WhenAny(Task[])

任何提供的任务已完成时,创建将完成的任务。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)

参数

tasks
Task[]

等待完成的任务。The tasks to wait on for completion.

返回

Task<Task>

表示提供的任务之一已完成的任务。A task that represents the completion of one of the supplied tasks. 返回任务的结果是完成的任务。The return task's Result is the task that completed.

例外

tasks 参数为 null。The tasks argument was null.

tasks 数组包含了 null 任务,或为空。The tasks array contained a null task, or was empty.

注解

当任何提供的任务完成时,将完成返回的任务。The returned task will complete when any of the supplied tasks has completed. 返回的任务将始终以 RanToCompletionResult 设置为要完成的第一个任务的状态结束。The returned task will always end in the RanToCompletion state with its Result set to the first task to complete. 即使完成的第一个任务以或状态结束,也是 Canceled 如此 FaultedThis is true even if the first task to complete ended in the Canceled or Faulted state.

适用于

WhenAny(Task, Task)

提供的任一任务完成时,创建将完成的任务。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)

参数

task1
Task

等待完成的第一个任务。The first task to wait on for completion.

task2
Task

等待完成的第二个任务。The second task to wait on for completion.

返回

Task<Task>

表示提供的任务之一已完成的新任务。A new task that represents the completion of one of the supplied tasks. Result 是首先完成的任务。Its Result is the task that completed first.

例外

task1task2nulltask1 or task2 was null.

注解

当任何提供的任务完成时,将完成返回的任务。The returned task will complete when any of the supplied tasks has completed. 返回的任务将始终以 RanToCompletionResult 设置为要完成的第一个任务的状态结束。The returned task will always end in the RanToCompletion state with its Result set to the first task to complete. true即使完成的第一个任务以或状态结束,结果值也是如此 Canceled FaultedThe result value is true even if the first task to complete ended in the Canceled or Faulted state.

适用于

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

任何提供的任务已完成时,创建将完成的任务。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))

类型参数

TResult

已完成任务的类型。The type of the completed task.

参数

tasks
IEnumerable<Task<TResult>>

等待完成的任务。The tasks to wait on for completion.

返回

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.

例外

tasks 参数为 nullThe tasks argument was null.

tasks 数组包含了 null 任务,或为空。The tasks array contained a null task, or was empty.

注解

当任何提供的任务完成时,将完成返回的任务。The returned task will complete when any of the supplied tasks has completed. 返回的任务将始终以 RanToCompletion 状态结束,并将其结果集设置为要完成的第一个任务。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.

适用于

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

任何提供的任务已完成时,创建将完成的任务。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))

类型参数

TResult

已完成任务的类型。The type of the completed task.

参数

tasks
Task<TResult>[]

等待完成的任务。The tasks to wait on for completion.

返回

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.

例外

tasks 参数为 null。The tasks argument was null.

tasks 数组包含了 null 任务,或为空。The tasks array contained a null task, or was empty.

注解

当任何提供的任务完成时,将完成返回的任务。The returned task will complete when any of the supplied tasks has completed. 返回的任务将始终以 RanToCompletion 状态结束,并将其结果集设置为要完成的第一个任务。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.

适用于

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

提供的任一任务完成时,创建将完成的任务。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))

类型参数

TResult

返回的任务的结果类型。The type of the result of the returned task.

参数

task1
Task<TResult>

等待完成的第一个任务。The first task to wait on for completion.

task2
Task<TResult>

等待完成的第二个任务。The second task to wait on for completion.

返回

Task<Task<TResult>>

表示提供的任务之一已完成的任务。A task that represents the completion of one of the supplied tasks. 返回的任务的 TResult 是第一项完成的任务。The returned task's TResult is the task that completed first.

例外

task1task2nulltask1 or task2 was null.

注解

当任何提供的任务完成时,将完成返回的任务。The returned task will complete when any of the supplied tasks has completed. 返回的任务将始终以 RanToCompletionResult 设置为要完成的第一个任务的状态结束。The returned task will always end in the RanToCompletion state with its Result set to the first task to complete. true即使完成的第一个任务以或状态结束,结果值也是如此 Canceled FaultedThe result value is true even if the first task to complete ended in the Canceled or Faulted state.

适用于