Task.WaitAll 方法

定义

等待提供的所有 Task 对象完成执行。

重载

WaitAll(Task[], Int32, CancellationToken)

等待提供的所有 Task 对象在指定的毫秒内完成执行,或等待取消为止。

WaitAll(ReadOnlySpan<Task>)

等待提供的所有 Task 对象完成执行。

WaitAll(Task[])

等待提供的所有 Task 对象完成执行。

WaitAll(IEnumerable<Task>, CancellationToken)

等待提供的所有 Task 对象完成执行,除非取消等待。

WaitAll(Task[], Int32)

等待提供的所有 Task 对象在指定的毫秒数内完成执行。

WaitAll(Task[], CancellationToken)

等待提供的所有 Task 对象完成执行,除非取消等待。

WaitAll(Task[], TimeSpan)

等待提供的所有可取消 Task 对象在指定的时间间隔内完成执行。

WaitAll(Task[], Int32, CancellationToken)

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

等待提供的所有 Task 对象在指定的毫秒内完成执行,或等待取消为止。

C#
public static bool WaitAll (System.Threading.Tasks.Task[] tasks, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool WaitAll (System.Threading.Tasks.Task[] tasks, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);

参数

tasks
Task[]

要等待的 Task 实例的数组。

millisecondsTimeout
Int32

等待的毫秒数,或 Infinite(-1)无限期等待。

cancellationToken
CancellationToken

等待任务完成时要观察的 CancellationToken

返回

如果所有 Task 实例在分配的时间内完成执行,则 true;否则,false

属性

例外

已释放 tasks 中的一个或多个 Task 对象。

tasks 参数 null

取消了至少一个 Task 实例。 如果任务已取消,则 AggregateException 在其 InnerExceptions 集合中包含 OperationCanceledException

-或-

在执行至少一个 Task 实例的过程中引发了异常。

millisecondsTimeout 是除 -1 以外的负数,表示无限超时。

tasks 参数包含 null 元素。

cancellationToken 已取消。

注解

cancellationToken 参数用于取消等待操作。 取消任务是一项不同的操作,由上述 AggregateException 发出信号。

适用于

.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.0, 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

WaitAll(ReadOnlySpan<Task>)

等待提供的所有 Task 对象完成执行。

C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void WaitAll (scoped ReadOnlySpan<System.Threading.Tasks.Task> tasks);

参数

tasks
ReadOnlySpan<Task>

要等待的 Task 实例的数组。

属性

例外

tasks 参数包含 null 元素。

取消了至少一个 Task 实例。

-或-

在执行至少一个 Task 实例的过程中引发了异常。

适用于

.NET 9
产品 版本
.NET 9

WaitAll(Task[])

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

等待提供的所有 Task 对象完成执行。

C#
public static void WaitAll (params System.Threading.Tasks.Task[] tasks);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void WaitAll (params System.Threading.Tasks.Task[] tasks);

参数

tasks
Task[]

要等待的 Task 实例的数组。

属性

例外

已释放 tasks 中的一个或多个 Task 对象。

tasks 参数 null

tasks 参数包含 null 元素。

取消了至少一个 Task 实例。 如果任务已取消,则 AggregateException 异常在其 InnerExceptions 集合中包含 OperationCanceledException 异常。

-或-

在执行至少一个 Task 实例的过程中引发了异常。

示例

以下示例启动 10 个任务,其中每个任务作为状态对象传递索引。 索引为 2 到 5 的任务会引发异常。 对 WaitAll 方法的调用包装 AggregateException 对象中的所有异常,并将其传播到调用线程。

C#
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

class Example
{
    static void Main()
    {
        var tasks = new List<Task<int>>();
         
        // Define a delegate that prints and returns the system tick count
        Func<object, int> action = (object obj) =>
        {
            int i = (int)obj;

            // Make each thread sleep a different time in order to return a different tick count
            Thread.Sleep(i * 100);

            // The tasks that receive an argument between 2 and 5 throw exceptions
            if (2 <= i && i <= 5)
            {
                throw new InvalidOperationException("SIMULATED EXCEPTION");
            }

            int tickCount = Environment.TickCount;
            Console.WriteLine("Task={0}, i={1}, TickCount={2}, Thread={3}", Task.CurrentId, i, tickCount, Thread.CurrentThread.ManagedThreadId);

            return tickCount;
        };

        // Construct started tasks
        for (int i = 0; i < 10; i++)
        {
            int index = i;
            tasks.Add(Task<int>.Factory.StartNew(action, index));
        }

        try
        {
            // Wait for all the tasks to finish.
            Task.WaitAll(tasks.ToArray());

            // We should never get to this point
            Console.WriteLine("WaitAll() has not thrown exceptions. THIS WAS NOT EXPECTED.");
        }
        catch (AggregateException e)
        {
            Console.WriteLine("\nThe following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)");
            for (int j = 0; j < e.InnerExceptions.Count; j++)
            {
                Console.WriteLine("\n-------------------------------------------------\n{0}", e.InnerExceptions[j].ToString());
            }
        }
    }
}
// The example displays output like the following:
//     Task=1, i=0, TickCount=1203822250, Thread=3
//     Task=2, i=1, TickCount=1203822359, Thread=4
//     Task=7, i=6, TickCount=1203823484, Thread=3
//     Task=8, i=7, TickCount=1203823890, Thread=4
//     Task=9, i=8, TickCount=1203824296, Thread=3
//     Task=10, i=9, TickCount=1203824796, Thread=4
//     
//     The following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)
//     
//     -------------------------------------------------
//     System.InvalidOperationException: SIMULATED EXCEPTION
//        at Example.<Main>b__0(Object obj)
//        at System.Threading.Tasks.Task`1.InnerInvoke()
//        at System.Threading.Tasks.Task.Execute()
//     
//     -------------------------------------------------
//     System.InvalidOperationException: SIMULATED EXCEPTION
//        at Example.<Main>b__0(Object obj)
//        at System.Threading.Tasks.Task`1.InnerInvoke()
//        at System.Threading.Tasks.Task.Execute()
//     
//     -------------------------------------------------
//     System.InvalidOperationException: SIMULATED EXCEPTION
//        at Example.<Main>b__0(Object obj)
//        at System.Threading.Tasks.Task`1.InnerInvoke()
//        at System.Threading.Tasks.Task.Execute()
//     
//     -------------------------------------------------
//     System.InvalidOperationException: SIMULATED EXCEPTION
//        at Example.<Main>b__0(Object obj)
//        at System.Threading.Tasks.Task`1.InnerInvoke()
//        at System.Threading.Tasks.Task.Execute()

适用于

.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.0, 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

WaitAll(IEnumerable<Task>, CancellationToken)

等待提供的所有 Task 对象完成执行,除非取消等待。

C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void WaitAll (System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task> tasks, System.Threading.CancellationToken cancellationToken = default);

参数

tasks
IEnumerable<Task>

要等待的任务实例的 IEnumerable<T>

cancellationToken
CancellationToken

等待任务完成时要观察的 System.Threading.Tasks.Task.CancellationToken

属性

例外

tasks 参数 null

tasks 参数包含 null 元素。

已释放任务中的一个或多个 Task 对象。

cancellationToken 已取消。

取消了至少一个 Task 实例。 如果任务已取消,则 AggregateException 在其 InnerExceptions 集合中包含 OperationCanceledException

适用于

.NET 9
产品 版本
.NET 9

WaitAll(Task[], Int32)

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

等待提供的所有 Task 对象在指定的毫秒数内完成执行。

C#
public static bool WaitAll (System.Threading.Tasks.Task[] tasks, int millisecondsTimeout);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool WaitAll (System.Threading.Tasks.Task[] tasks, int millisecondsTimeout);

参数

tasks
Task[]

要等待的 Task 实例的数组。

millisecondsTimeout
Int32

等待的毫秒数,或 Infinite(-1)无限期等待。

返回

如果所有 Task 实例在分配的时间内完成执行,则 true;否则,false

属性

例外

已释放 tasks 中的一个或多个 Task 对象。

tasks 参数 null

取消了至少一个 Task 实例。 如果任务已取消,则 AggregateException 在其 InnerExceptions 集合中包含 OperationCanceledException

-或-

在执行至少一个 Task 实例的过程中引发了异常。

millisecondsTimeout 是除 -1 以外的负数,表示无限超时。

tasks 参数包含 null 元素。

适用于

.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.0, 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

WaitAll(Task[], CancellationToken)

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

等待提供的所有 Task 对象完成执行,除非取消等待。

C#
public static void WaitAll (System.Threading.Tasks.Task[] tasks, System.Threading.CancellationToken cancellationToken);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void WaitAll (System.Threading.Tasks.Task[] tasks, System.Threading.CancellationToken cancellationToken);

参数

tasks
Task[]

要等待的 Task 实例的数组。

cancellationToken
CancellationToken

等待任务完成时要观察的 CancellationToken

属性

例外

cancellationToken 已取消。

tasks 参数 null

取消了至少一个 Task 实例。 如果任务已取消,则 AggregateException 在其 InnerExceptions 集合中包含 OperationCanceledException

-或-

在执行至少一个 Task 实例的过程中引发了异常。

tasks 参数包含 null 元素。

已释放 tasks 中的一个或多个 Task 对象。

注解

cancellationToken 参数用于取消等待操作。 取消任务是一项不同的操作,由上述 AggregateException 发出信号。

适用于

.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.0, 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

WaitAll(Task[], TimeSpan)

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

等待提供的所有可取消 Task 对象在指定的时间间隔内完成执行。

C#
public static bool WaitAll (System.Threading.Tasks.Task[] tasks, TimeSpan timeout);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool WaitAll (System.Threading.Tasks.Task[] tasks, TimeSpan timeout);

参数

tasks
Task[]

要等待的 Task 实例的数组。

timeout
TimeSpan

表示等待的毫秒数或表示无限期等待 -1 毫秒的 TimeSpanTimeSpan

返回

如果所有 Task 实例在分配的时间内完成执行,则 true;否则,false

属性

例外

已释放 tasks 中的一个或多个 Task 对象。

tasks 参数 null

取消了至少一个 Task 实例。 如果任务已取消,则 AggregateException 在其 InnerExceptions 集合中包含 OperationCanceledException

-或-

在执行至少一个 Task 实例的过程中引发了异常。

timeout 是一个负数,而不是 -1 毫秒,表示无限超时。

-或-

timeout 大于 Int32.MaxValue

tasks 参数包含 null 元素。

适用于

.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.0, 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