Task.WhenAll 方法

定义

创建一个任务,该任务将在所有提供的任务完成时完成。

重载

WhenAll(IEnumerable<Task>)

创建一个任务,该任务将在可枚举集合中的所有 Task 对象完成时完成。

WhenAll(ReadOnlySpan<Task>)

创建一个任务,该任务将在所有提供的任务完成时完成。

WhenAll(Task[])

创建一个任务,该任务将在数组中的所有 Task 对象完成时完成。

WhenAll<TResult>(ReadOnlySpan<Task<TResult>>)

创建一个任务,该任务将在所有提供的任务完成时完成。

WhenAll<TResult>(IEnumerable<Task<TResult>>)

创建一个任务,该任务将在可枚举集合中的所有 Task<TResult> 对象完成时完成。

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

创建一个任务,该任务将在数组中的所有 Task<TResult> 对象完成时完成。

WhenAll(IEnumerable<Task>)

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

创建一个任务,该任务将在可枚举集合中的所有 Task 对象完成时完成。

public:
 static System::Threading::Tasks::Task ^ WhenAll(System::Collections::Generic::IEnumerable<System::Threading::Tasks::Task ^> ^ tasks);
public static System.Threading.Tasks.Task WhenAll (System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task> tasks);
static member WhenAll : seq<System.Threading.Tasks.Task> -> System.Threading.Tasks.Task
Public Shared Function WhenAll (tasks As IEnumerable(Of Task)) As Task

参数

tasks
IEnumerable<Task>

要等待完成的任务。

返回

表示所有提供任务的完成的任务。

例外

tasks 参数 null

tasks 集合包含 null 任务。

示例

以下示例创建一组任务,用于对数组中的 URL 执行 ping 操作。 任务存储在传递给 WhenAll(IEnumerable<Task>) 方法的 List<Task> 集合中。 调用 Wait 方法可确保所有线程都已完成之后,该示例将检查 Task.Status 属性以确定是否有任何任务出错。

using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      int failed = 0;
      var tasks = new List<Task>();
      String[] urls = { "www.adatum.com", "www.cohovineyard.com",
                        "www.cohowinery.com", "www.northwindtraders.com",
                        "www.contoso.com" };
      
      foreach (var value in urls) {
         var url = value;
         tasks.Add(Task.Run( () => { var png = new Ping();
                                     try {
                                        var reply = png.Send(url);
                                        if (! (reply.Status == IPStatus.Success)) {
                                           Interlocked.Increment(ref failed);
                                           throw new TimeoutException("Unable to reach " + url + ".");
                                        }
                                     }
                                     catch (PingException) {
                                        Interlocked.Increment(ref failed);
                                        throw;
                                     }
                                   }));
      }
      Task t = Task.WhenAll(tasks);
      try {
         t.Wait();
      }
      catch {}   

      if (t.Status == TaskStatus.RanToCompletion)
         Console.WriteLine("All ping attempts succeeded.");
      else if (t.Status == TaskStatus.Faulted)
         Console.WriteLine("{0} ping attempts failed", failed);      
   }
}
// The example displays output like the following:
//       5 ping attempts failed
open System
open System.Net.NetworkInformation
open System.Threading
open System.Threading.Tasks

let mutable failed = 0

let urls =
    [ "www.adatum.com"
      "www.cohovineyard.com"
      "www.cohowinery.com"
      "www.northwindtraders.com"
      "www.contoso.com" ]

let tasks =
    urls
    |> List.map (fun url ->
        Task.Run(fun () ->
            let png = new Ping()

            try
                let reply = png.Send url

                if reply.Status <> IPStatus.Success then
                    Interlocked.Increment &failed |> ignore
                    raise (TimeoutException $"Unable to reach {url}.")
            with :? PingException ->
                Interlocked.Increment &failed |> ignore
                reraise ()))

let t = Task.WhenAll tasks

try
    t.Wait()
with _ ->
    ()

if t.Status = TaskStatus.RanToCompletion then
    printfn "All ping attempts succeeded."
elif t.Status = TaskStatus.Faulted then
    printfn $"{failed} ping attempts failed"

// The example displays output like the following:
//       5 ping attempts failed
Imports System.Collections.Generic
Imports System.Net.NetworkInformation
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim failed As Integer = 0
      Dim tasks As New List(Of Task)()
      Dim urls() As String = { "www.adatum.com", "www.cohovineyard.com",
                              "www.cohowinery.com", "www.northwindtraders.com",
                              "www.contoso.com" }
      
      For Each value In urls
         Dim url As String = value
         tasks.Add(Task.Run( Sub()
                                Dim png As New Ping()
                                Try
                                   Dim reply = png.Send(url)
                                   If Not reply.Status = IPStatus.Success Then
                                      Interlocked.Increment(failed)
                                      Throw New TimeoutException("Unable to reach " + url + ".")
                                   End If
                                   Catch e As PingException
                                      Interlocked.Increment(failed)
                                      Throw
                                   End Try
                             End Sub))
      Next
      Dim t As Task = Task.WhenAll(tasks)
      Try
         t.Wait()
      Catch
      End Try   

      If t.Status = TaskStatus.RanToCompletion
         Console.WriteLine("All ping attempts succeeded.")
      ElseIf t.Status = TaskStatus.Faulted
         Console.WriteLine("{0} ping attempts failed", failed)      
      End If
   End Sub
End Module
' The example displays output like the following:
'     5 ping attempts failed

注解

当对一组任务的状态或一组任务引发的异常感兴趣时,通常会调用返回 Task 对象的 WhenAll 方法的重载。

注意

WhenAll(IEnumerable<Task>) 方法的调用不会阻止调用线程。

如果提供的任何任务都以错误状态完成,则返回的任务也将处于 Faulted 状态,其中其异常将包含每个提供的任务的未包装异常集的聚合。

如果提供的任务均未出错,但其中至少一个任务已取消,则返回的任务将以 Canceled 状态结束。

如果没有任何任务出错且未取消任何任务,则生成的任务将以 RanToCompletion 状态结束。

如果提供的数组/可枚举不包含任何任务,则返回的任务将在返回给调用方之前立即转换为 RanToCompletion 状态。

适用于

WhenAll(ReadOnlySpan<Task>)

创建一个任务,该任务将在所有提供的任务完成时完成。

public:
 static System::Threading::Tasks::Task ^ WhenAll(ReadOnlySpan<System::Threading::Tasks::Task ^> tasks);
public static System.Threading.Tasks.Task WhenAll (scoped ReadOnlySpan<System.Threading.Tasks.Task> tasks);
static member WhenAll : ReadOnlySpan<System.Threading.Tasks.Task> -> System.Threading.Tasks.Task
Public Shared Function WhenAll (tasks As ReadOnlySpan(Of Task)) As Task

参数

tasks
ReadOnlySpan<Task>

要等待完成的任务。

返回

表示所有提供任务的完成的任务。

例外

tasks 数组包含 null 任务。

注解

如果提供的任何任务都以错误状态完成,则返回的任务也将处于“出错”状态,其中异常将包含每个提供任务中未包装的异常集的聚合。

如果提供的任务均未出错,但至少其中一个任务已取消,则返回的任务将以“已取消”状态结束。

如果没有任何任务出错且未取消任何任务,则生成的任务将以 RanToCompletion 状态结束。

如果提供的跨度不包含任何任务,则返回的任务将在返回给调用方之前立即转换为 RanToCompletion 状态。

适用于

WhenAll(Task[])

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

创建一个任务,该任务将在数组中的所有 Task 对象完成时完成。

public:
 static System::Threading::Tasks::Task ^ WhenAll(... cli::array <System::Threading::Tasks::Task ^> ^ tasks);
public static System.Threading.Tasks.Task WhenAll (params System.Threading.Tasks.Task[] tasks);
static member WhenAll : System.Threading.Tasks.Task[] -> System.Threading.Tasks.Task
Public Shared Function WhenAll (ParamArray tasks As Task()) As Task

参数

tasks
Task[]

要等待完成的任务。

返回

表示所有提供任务的完成的任务。

例外

tasks 参数 null

tasks 数组包含 null 任务。

示例

以下示例创建一组任务,用于对数组中的 URL 执行 ping 操作。 任务存储在 List<Task> 集合中,该集合转换为数组并传递给 WhenAll(IEnumerable<Task>) 方法。 调用 Wait 方法可确保所有线程都已完成之后,该示例将检查 Task.Status 属性以确定是否有任何任务出错。

using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static async Task Main()
   {
      int failed = 0;
      var tasks = new List<Task>();
      String[] urls = { "www.adatum.com", "www.cohovineyard.com",
                        "www.cohowinery.com", "www.northwindtraders.com",
                        "www.contoso.com" };
      
      foreach (var value in urls) {
         var url = value;
         tasks.Add(Task.Run( () => { var png = new Ping();
                                     try {
                                        var reply = png.Send(url);
                                        if (! (reply.Status == IPStatus.Success)) {
                                           Interlocked.Increment(ref failed);
                                           throw new TimeoutException("Unable to reach " + url + ".");
                                        }
                                     }
                                     catch (PingException) {
                                        Interlocked.Increment(ref failed);
                                        throw;
                                     }
                                   }));
      }
      Task t = Task.WhenAll(tasks.ToArray());
      try {
         await t;
      }
      catch {}   

      if (t.Status == TaskStatus.RanToCompletion)
         Console.WriteLine("All ping attempts succeeded.");
      else if (t.Status == TaskStatus.Faulted)
         Console.WriteLine("{0} ping attempts failed", failed);      
   }
}
// The example displays output like the following:
//       5 ping attempts failed
open System
open System.Net.NetworkInformation
open System.Threading
open System.Threading.Tasks

let mutable failed = 0

let urls =
    [| "www.adatum.com"
       "www.cohovineyard.com"
       "www.cohowinery.com"
       "www.northwindtraders.com"
       "www.contoso.com" |]

let tasks =
    urls
    |> Array.map (fun url ->
        Task.Run(fun () ->
            let png = new Ping()

            try
                let reply = png.Send url

                if reply.Status <> IPStatus.Success then
                    Interlocked.Increment &failed |> ignore
                    raise (TimeoutException $"Unable to reach {url}.")
            with :? PingException ->
                Interlocked.Increment &failed |> ignore
                reraise ()))

let main =
    task {
        let t = Task.WhenAll tasks

        try
            do! t
        with _ ->
            ()

        if t.Status = TaskStatus.RanToCompletion then
            printfn "All ping attempts succeeded."
        elif t.Status = TaskStatus.Faulted then
            printfn $"{failed} ping attempts failed"
    }

main.Wait()
// The example displays output like the following:
//       5 ping attempts failed
Imports System.Collections.Generic
Imports System.Net.NetworkInformation
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim failed As Integer = 0
      Dim tasks As New List(Of Task)()
      Dim urls() As String = { "www.adatum.com", "www.cohovineyard.com",
                              "www.cohowinery.com", "www.northwindtraders.com",
                              "www.contoso.com" }
      
      For Each value In urls
         Dim url As String = value
         tasks.Add(Task.Run( Sub()
                                Dim png As New Ping()
                                Try
                                   Dim reply = png.Send(url)
                                   If Not reply.Status = IPStatus.Success Then
                                      Interlocked.Increment(failed)
                                      Throw New TimeoutException("Unable to reach " + url + ".")
                                   End If
                                   Catch e As PingException
                                      Interlocked.Increment(failed)
                                      Throw
                                   End Try
                             End Sub))
      Next
      Dim t As Task = Task.WhenAll(tasks.ToArray())
      Try
         t.Wait()
      Catch
      End Try   

      If t.Status = TaskStatus.RanToCompletion
         Console.WriteLine("All ping attempts succeeded.")
      ElseIf t.Status = TaskStatus.Faulted
         Console.WriteLine("{0} ping attempts failed", failed)      
      End If
   End Sub
End Module
' The example displays output like the following:
'     5 ping attempts failed

注解

当对一组任务的状态或一组任务引发的异常感兴趣时,通常会调用返回 Task 对象的 WhenAll 方法的重载。

注意

WhenAll(Task[]) 方法的调用不会阻止调用线程。

如果提供的任何任务都以错误状态完成,则返回的任务也将处于 Faulted 状态,其中其异常将包含每个提供的任务的未包装异常集的聚合。

如果提供的任务均未出错,但其中至少一个任务已取消,则返回的任务将以 Canceled 状态结束。

如果没有任何任务出错且未取消任何任务,则生成的任务将以 RanToCompletion 状态结束。

如果提供的数组/可枚举不包含任何任务,则返回的任务将在返回给调用方之前立即转换为 RanToCompletion 状态。

适用于

WhenAll<TResult>(ReadOnlySpan<Task<TResult>>)

创建一个任务,该任务将在所有提供的任务完成时完成。

public:
generic <typename TResult>
 static System::Threading::Tasks::Task<cli::array <TResult> ^> ^ WhenAll(ReadOnlySpan<System::Threading::Tasks::Task<TResult> ^> tasks);
public static System.Threading.Tasks.Task<TResult[]> WhenAll<TResult> (scoped ReadOnlySpan<System.Threading.Tasks.Task<TResult>> tasks);
static member WhenAll : ReadOnlySpan<System.Threading.Tasks.Task<'Result>> -> System.Threading.Tasks.Task<'Result[]>
Public Shared Function WhenAll(Of TResult) (tasks As ReadOnlySpan(Of Task(Of TResult))) As Task(Of TResult())

类型参数

TResult

参数

tasks
ReadOnlySpan<Task<TResult>>

要等待完成的任务。

返回

Task<TResult[]>

表示所有提供任务的完成的任务。

例外

tasks 数组包含 null 任务。

注解

如果提供的任何任务都以错误状态完成,则返回的任务也将处于“出错”状态,其中异常将包含每个提供任务中未包装的异常集的聚合。

如果提供的任务均未出错,但至少其中一个任务已取消,则返回的任务将以“已取消”状态结束。

如果没有任何任务出错且未取消任何任务,则生成的任务将以 RanToCompletion 状态结束。 返回的任务的结果将设置为一个数组,该数组包含所提供的任务的所有结果,其顺序与提供的顺序相同(例如,如果输入任务数组包含 t1、t2、t3,则输出任务的结果将返回一个 TResult[],其中 arr[0] == t1)。Result, arr[1] == t2。结果和 arr[2] == t3。结果)。

如果提供的数组/可枚举不包含任何任务,则返回的任务将在返回给调用方之前立即转换为 RanToCompletion 状态。 返回的 TResult[] 将是 0 个元素的数组。

适用于

WhenAll<TResult>(IEnumerable<Task<TResult>>)

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

创建一个任务,该任务将在可枚举集合中的所有 Task<TResult> 对象完成时完成。

public:
generic <typename TResult>
 static System::Threading::Tasks::Task<cli::array <TResult> ^> ^ WhenAll(System::Collections::Generic::IEnumerable<System::Threading::Tasks::Task<TResult> ^> ^ tasks);
public static System.Threading.Tasks.Task<TResult[]> WhenAll<TResult> (System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task<TResult>> tasks);
static member WhenAll : seq<System.Threading.Tasks.Task<'Result>> -> System.Threading.Tasks.Task<'Result[]>
Public Shared Function WhenAll(Of TResult) (tasks As IEnumerable(Of Task(Of TResult))) As Task(Of TResult())

类型参数

TResult

已完成任务的类型。

参数

tasks
IEnumerable<Task<TResult>>

要等待完成的任务。

返回

Task<TResult[]>

表示所有提供任务的完成的任务。

例外

tasks 参数 null

tasks 集合包含 null 任务。

示例

以下示例创建 10 个任务,每个任务实例化一个随机数生成器,该生成器在 1 到 1,000 之间创建 1,000 个随机数并计算其平均值。 Delay(Int32) 方法用于延迟随机数生成器的实例化,以便不会使用相同的种子值创建它们。 然后,对 WhenAll 方法的调用返回一个 Int64 数组,其中包含每个任务计算的平均值。 然后,这些值用于计算总体平均值。

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

public class Example
{
   public static void Main()
   {
      var tasks = new List<Task<long>>();
      for (int ctr = 1; ctr <= 10; ctr++) {
         int delayInterval = 18 * ctr;
         tasks.Add(Task.Run(async () => { long total = 0;
                                          await Task.Delay(delayInterval);
                                          var rnd = new Random();
                                          // Generate 1,000 random numbers.
                                          for (int n = 1; n <= 1000; n++)
                                             total += rnd.Next(0, 1000);
                                          return total; } ));
      }
      var continuation = Task.WhenAll(tasks);
      try {
         continuation.Wait();
      }
      catch (AggregateException)
      { }
   
      if (continuation.Status == TaskStatus.RanToCompletion) {
         long grandTotal = 0;
         foreach (var result in continuation.Result) {
            grandTotal += result;
            Console.WriteLine("Mean: {0:N2}, n = 1,000", result/1000.0);
         }
   
         Console.WriteLine("\nMean of Means: {0:N2}, n = 10,000",
                           grandTotal/10000);
      }
      // Display information on faulted tasks.
      else {
         foreach (var t in tasks) {
            Console.WriteLine("Task {0}: {1}", t.Id, t.Status);
         }
      }
   }
}
// The example displays output like the following:
//       Mean: 506.34, n = 1,000
//       Mean: 504.69, n = 1,000
//       Mean: 489.32, n = 1,000
//       Mean: 505.96, n = 1,000
//       Mean: 515.31, n = 1,000
//       Mean: 499.94, n = 1,000
//       Mean: 496.92, n = 1,000
//       Mean: 508.58, n = 1,000
//       Mean: 494.88, n = 1,000
//       Mean: 493.53, n = 1,000
//
//       Mean of Means: 501.55, n = 10,000
open System
open System.Threading.Tasks

let tasks =
    [| for i = 1 to 10 do
           let delayInterval = 18 * i

           task {
               let mutable total = 0L
               do! Task.Delay delayInterval
               let rnd = Random()

               for _ = 1 to 1000 do
                   total <- total + (rnd.Next(0, 1000) |> int64)

               return total
           } |]

let continuation = Task.WhenAll tasks

try
    continuation.Wait()

with :? AggregateException ->
    ()

if continuation.Status = TaskStatus.RanToCompletion then
    for result in continuation.Result do
        printfn $"Mean: {float result / 1000.:N2}, n = 1,000"

    let grandTotal = continuation.Result |> Array.sum

    printfn $"\nMean of Means: {float grandTotal / 10000.:N2}, n = 10,000"

// Display information on faulted tasks.
else
    for t in tasks do
        printfn $"Task {t.Id}: {t.Status}"

// The example displays output like the following:
//       Mean: 506.34, n = 1,000
//       Mean: 504.69, n = 1,000
//       Mean: 489.32, n = 1,000
//       Mean: 505.96, n = 1,000
//       Mean: 515.31, n = 1,000
//       Mean: 499.94, n = 1,000
//       Mean: 496.92, n = 1,000
//       Mean: 508.58, n = 1,000
//       Mean: 494.88, n = 1,000
//       Mean: 493.53, n = 1,000
//
//       Mean of Means: 501.55, n = 10,000
Imports System.Collections.Generic
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim tasks As New List(Of Task(Of Long))()
      For ctr As Integer = 1 To 10
         Dim delayInterval As Integer = 18 * ctr
         tasks.Add(Task.Run(Async Function()
                               Dim total As Long = 0
                               Await Task.Delay(delayInterval)
                               Dim rnd As New Random()
                               ' Generate 1,000 random numbers.
                               For n As Integer = 1 To 1000
                                  total += rnd.Next(0, 1000)
                               Next
                               Return total
                            End Function))
      Next
      Dim continuation = Task.WhenAll(tasks)
      Try
         continuation.Wait()
      Catch ae As AggregateException
      End Try
      
      If continuation.Status = TaskStatus.RanToCompletion Then
         Dim grandTotal As Long = 0
         For Each result in continuation.Result
            grandTotal += result
            Console.WriteLine("Mean: {0:N2}, n = 1,000", result/1000)
         Next
         Console.WriteLine()
         Console.WriteLine("Mean of Means: {0:N2}, n = 10,000",
                           grandTotal/10000)
      ' Display information on faulted tasks.
      Else 
         For Each t In tasks
            Console.WriteLine("Task {0}: {1}", t.Id, t.Status)
         Next
      End If
   End Sub
End Module
' The example displays output like the following:
'       Mean: 506.34, n = 1,000
'       Mean: 504.69, n = 1,000
'       Mean: 489.32, n = 1,000
'       Mean: 505.96, n = 1,000
'       Mean: 515.31, n = 1,000
'       Mean: 499.94, n = 1,000
'       Mean: 496.92, n = 1,000
'       Mean: 508.58, n = 1,000
'       Mean: 494.88, n = 1,000
'       Mean: 493.53, n = 1,000
'
'       Mean of Means: 501.55, n = 10,000

在这种情况下,十个单个任务存储在 List<T> 对象中。 List<T> 实现 IEnumerable<T> 接口。

注解

WhenAll<TResult>(IEnumerable<Task<TResult>>) 方法的调用不会阻止调用线程。 但是,对返回 Result 属性的调用确实会阻止调用线程。

如果提供的任何任务都以错误状态完成,则返回的任务也将处于 Faulted 状态,其中其异常将包含每个提供的任务的未包装异常集的聚合。

如果提供的任务均未出错,但其中至少一个任务已取消,则返回的任务将以 Canceled 状态结束。

如果没有任何任务出错且未取消任何任务,则生成的任务将以 RanToCompletion 状态结束。 返回任务的 Task<TResult>.Result 属性将设置为一个数组,该数组包含所提供的任务的所有结果,其顺序与提供的顺序相同(例如,如果输入任务数组包含 t1、t2、t3,则输出任务的 Task<TResult>.Result 属性将返回一个 TResult[],其中 arr[0] == t1.Result, arr[1] == t2.Result, and arr[2] == t3.Result)

如果 tasks 参数不包含任何任务,则返回的任务将在返回给调用方之前立即转换为 RanToCompletion 状态。 返回的 TResult[] 将是 0 个元素的数组。

适用于

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

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

创建一个任务,该任务将在数组中的所有 Task<TResult> 对象完成时完成。

public:
generic <typename TResult>
 static System::Threading::Tasks::Task<cli::array <TResult> ^> ^ WhenAll(... cli::array <System::Threading::Tasks::Task<TResult> ^> ^ tasks);
public static System.Threading.Tasks.Task<TResult[]> WhenAll<TResult> (params System.Threading.Tasks.Task<TResult>[] tasks);
static member WhenAll : System.Threading.Tasks.Task<'Result>[] -> System.Threading.Tasks.Task<'Result[]>
Public Shared Function WhenAll(Of TResult) (ParamArray tasks As Task(Of TResult)()) As Task(Of TResult())

类型参数

TResult

已完成任务的类型。

参数

tasks
Task<TResult>[]

要等待完成的任务。

返回

Task<TResult[]>

表示所有提供任务的完成的任务。

例外

tasks 参数 null

tasks 数组包含 null 任务。

示例

以下示例创建 10 个任务,每个任务实例化一个随机数生成器,该生成器在 1 到 1,000 之间创建 1,000 个随机数并计算其平均值。 在这种情况下,十个单个任务存储在 Task<Int64> 数组中。 Delay(Int32) 方法用于延迟随机数生成器的实例化,以便不会使用相同的种子值创建它们。 然后,对 WhenAll 方法的调用返回一个 Int64 数组,其中包含每个任务计算的平均值。 然后,这些值用于计算总体平均值。

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

public class Example
{
   public static void Main()
   {
      var tasks = new Task<long>[10];
      for (int ctr = 1; ctr <= 10; ctr++) {
         int delayInterval = 18 * ctr;
         tasks[ctr - 1] = Task.Run(async () => { long total = 0;
                                                 await Task.Delay(delayInterval);
                                                 var rnd = new Random();
                                                 // Generate 1,000 random numbers.
                                                 for (int n = 1; n <= 1000; n++)
                                                    total += rnd.Next(0, 1000);

                                                 return total; } );
      }
      var continuation = Task.WhenAll(tasks);
      try {
         continuation.Wait();
      }
      catch (AggregateException)
      {}
   
      if (continuation.Status == TaskStatus.RanToCompletion) {
         long grandTotal = 0;
         foreach (var result in continuation.Result) {
            grandTotal += result;
            Console.WriteLine("Mean: {0:N2}, n = 1,000", result/1000.0);
         }
   
         Console.WriteLine("\nMean of Means: {0:N2}, n = 10,000",
                           grandTotal/10000);
      }
      // Display information on faulted tasks.
      else { 
         foreach (var t in tasks)
            Console.WriteLine("Task {0}: {1}", t.Id, t.Status);
      }
   }
}
// The example displays output like the following:
//       Mean: 506.38, n = 1,000
//       Mean: 501.01, n = 1,000
//       Mean: 505.36, n = 1,000
//       Mean: 492.00, n = 1,000
//       Mean: 508.36, n = 1,000
//       Mean: 503.99, n = 1,000
//       Mean: 504.95, n = 1,000
//       Mean: 508.58, n = 1,000
//       Mean: 490.23, n = 1,000
//       Mean: 501.59, n = 1,000
//
//       Mean of Means: 502.00, n = 10,000
open System
open System.Threading.Tasks

let tasks =
    [| for i = 1 to 10 do
           let delayInterval = 18 * i

           task {
               let mutable total = 0L
               do! Task.Delay delayInterval
               let rnd = Random()

               for _ = 1 to 1000 do
                   total <- total + (rnd.Next(0, 1000) |> int64)

               return total
           } |]

let continuation = Task.WhenAll tasks

try
    continuation.Wait()

with :? AggregateException ->
    ()

if continuation.Status = TaskStatus.RanToCompletion then
    for result in continuation.Result do
        printfn $"Mean: {float result / 1000.:N2}, n = 1,000"

    let grandTotal = Array.sum continuation.Result

    printfn $"\nMean of Means: {float grandTotal / 10000.:N2}, n = 10,000"

// Display information on faulted tasks.
else
    for t in tasks do
        printfn $"Task {t.Id}: {t.Status}"

// The example displays output like the following:
//       Mean: 506.38, n = 1,000
//       Mean: 501.01, n = 1,000
//       Mean: 505.36, n = 1,000
//       Mean: 492.00, n = 1,000
//       Mean: 508.36, n = 1,000
//       Mean: 503.99, n = 1,000
//       Mean: 504.95, n = 1,000
//       Mean: 508.58, n = 1,000
//       Mean: 490.23, n = 1,000
//       Mean: 501.59, n = 1,000
//
//       Mean of Means: 502.00, n = 10,000
Imports System.Collections.Generic
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim tasks(9) As Task(Of Long)
      For ctr As Integer = 1 To 10
         Dim delayInterval As Integer = 18 * ctr
         tasks(ctr - 1) =Task.Run(Async Function()
                                     Dim total As Long = 0
                                     Await Task.Delay(delayInterval)
                                     Dim rnd As New Random()
                                     ' Generate 1,000 random numbers.
                                     For n As Integer = 1 To 1000
                                        total += rnd.Next(0, 1000)
                                     Next
                                     Return total
                                  End Function)
      Next
      Dim continuation = Task.WhenAll(tasks)
      Try
         continuation.Wait()
      Catch ae As AggregateException
      End Try
         
      If continuation.Status = TaskStatus.RanToCompletion Then
         Dim grandTotal As Long = 0
         For Each result in continuation.Result
            grandTotal += result
            Console.WriteLine("Mean: {0:N2}, n = 1,000", result/1000)
         Next
         Console.WriteLine()
         Console.WriteLine("Mean of Means: {0:N2}, n = 10,000",
                           grandTotal/10000)
      ' Display information on faulted tasks.
      Else 
         For Each t In tasks
            Console.WriteLine("Task {0}: {1}", t.Id, t.Status)
         Next
      End If
   End Sub
End Module
' The example displays output like the following:
'       Mean: 506.38, n = 1,000
'       Mean: 501.01, n = 1,000
'       Mean: 505.36, n = 1,000
'       Mean: 492.00, n = 1,000
'       Mean: 508.36, n = 1,000
'       Mean: 503.99, n = 1,000
'       Mean: 504.95, n = 1,000
'       Mean: 508.58, n = 1,000
'       Mean: 490.23, n = 1,000
'       Mean: 501.59, n = 1,000
'
'       Mean of Means: 502.00, n = 10,000

注解

WhenAll<TResult>(Task<TResult>[]) 方法的调用不会阻止调用线程。 但是,对返回 Result 属性的调用确实会阻止调用线程。

如果提供的任何任务都以错误状态完成,则返回的任务也将处于 Faulted 状态,其中其异常将包含每个提供的任务的未包装异常集的聚合。

如果提供的任务均未出错,但其中至少一个任务已取消,则返回的任务将以 Canceled 状态结束。

如果没有任何任务出错且未取消任何任务,则生成的任务将以 RanToCompletion 状态结束。 返回任务的 Result 将设置为一个数组,该数组包含所提供的任务的所有结果,其顺序与提供的顺序相同(例如,如果输入任务数组包含 t1、t2、t3,则输出任务的 Result 将返回 TResult[],其中 arr[0] == t1.Result, arr[1] == t2.Result, and arr[2] == t3.Result)

如果提供的数组/可枚举不包含任何任务,则返回的任务将在返回给调用方之前立即转换为 RanToCompletion 状态。 返回的 TResult[] 将是 0 个元素的数组。

适用于