Task.WhenAny Method
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.
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>) |
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<TResult>(IEnumerable<Task<TResult>>) |
Creates a task that will complete when any of the supplied tasks have completed. |
WhenAny<TResult>(ReadOnlySpan<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. |
- 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
.NET 9 и други версии
Продукт | Версии |
---|---|
.NET | 5, 6, 7, 8, 9 |
- 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
.NET 9 и други версии
Продукт | Версии |
---|---|
.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 |
.NET Framework | 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 |
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
- tasks
- ReadOnlySpan<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
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
.NET 9
Продукт | Версии |
---|---|
.NET | 9 |
- 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
.NET 9 и други версии
Продукт | Версии |
---|---|
.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 |
.NET Framework | 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 |
- 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
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
.NET 9 и други версии
Продукт | Версии |
---|---|
.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 |
.NET Framework | 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 |
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
- tasks
- ReadOnlySpan<Task<TResult>>
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
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
.NET 9
Продукт | Версии |
---|---|
.NET | 9 |
- 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
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
.NET 9 и други версии
Продукт | Версии |
---|---|
.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 |
.NET Framework | 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 |
- 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
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.
Съвет
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
.NET 9 и други версии
Продукт | Версии |
---|---|
.NET | 5, 6, 7, 8, 9 |
Обратна връзка за .NET
.NET е проект с отворен код. Изберете връзка, за да предоставите обратна връзка: