Прочетете на английски Редактиране

Споделяне чрез


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>)

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.

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);

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

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 (params System.Threading.Tasks.Task[] tasks);

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

WhenAny(ReadOnlySpan<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 (scoped ReadOnlySpan<System.Threading.Tasks.Task> tasks);

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

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);

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

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 static System.Threading.Tasks.Task<System.Threading.Tasks.Task<TResult>> WhenAny<TResult> (System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task<TResult>> tasks);

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

.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

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

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

public static System.Threading.Tasks.Task<System.Threading.Tasks.Task<TResult>> WhenAny<TResult> (scoped ReadOnlySpan<System.Threading.Tasks.Task<TResult>> tasks);

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

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 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

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 static System.Threading.Tasks.Task<System.Threading.Tasks.Task<TResult>> WhenAny<TResult> (params System.Threading.Tasks.Task<TResult>[] tasks);

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

.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

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 static System.Threading.Tasks.Task<System.Threading.Tasks.Task<TResult>> WhenAny<TResult> (System.Threading.Tasks.Task<TResult> task1, System.Threading.Tasks.Task<TResult> task2);

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.

Съвет

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