Task.WaitAll 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
等待所有提供 Task 的对象完成执行。
重载
| 名称 | 说明 |
|---|---|
| WaitAll(Task[]) |
等待所有提供 Task 的对象完成执行。 |
| WaitAll(Task[], Int32) |
等待提供 Task 的所有对象在指定的毫秒数内完成执行。 |
| WaitAll(Task[], CancellationToken) |
等待所有提供 Task 的对象完成执行,除非取消等待。 |
| WaitAll(Task[], TimeSpan) |
等待提供的所有可 Task 取消对象在指定的时间间隔内完成执行。 |
| WaitAll(Task[], Int32, CancellationToken) |
等待所有提供 Task 的对象在指定的毫秒内完成执行,或直到取消等待为止。 |
WaitAll(Task[])
等待所有提供 Task 的对象完成执行。
public:
static void WaitAll(... cli::array <System::Threading::Tasks::Task ^> ^ tasks);
public static void WaitAll(params System.Threading.Tasks.Task[] tasks);
static member WaitAll : System.Threading.Tasks.Task[] -> unit
Public Shared Sub WaitAll (ParamArray tasks As Task())
参数
例外
已释放其中的tasks一个或多个Task对象。
参数 tasks 为 null.
该 tasks 参数包含 null 元素。
至少取消了其中一个 Task 实例。 如果任务已取消,则 AggregateException 异常 OperationCanceledException 在其 InnerExceptions 集合中包含异常。
-或-
在执行至少一个 Task 实例的过程中引发了异常。
示例
以下示例启动 10 个任务,其中每个任务作为状态对象传递索引。 索引为 2 到 5 的任务会引发异常。 对方法的调用 WaitAll 包装对象 AggregateException 中的所有异常,并将其传播到调用线程。
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()
open System
open System.Threading
open System.Threading.Tasks
// Define a delegate that prints and returns the system tick count
let action =
fun (obj: obj) ->
let i = obj :?> int
// 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 then
raise (InvalidOperationException "SIMULATED EXCEPTION")
let tickCount = Environment.TickCount
printfn $"Task={Task.CurrentId}, i={i}, TickCount={tickCount}, Thread={Thread.CurrentThread.ManagedThreadId}"
tickCount
// Construct started tasks
let tasks =
[| for i = 0 to 9 do
Task<int>.Factory.StartNew (action, i) |]
try
// Wait for all the tasks to finish.
Seq.cast tasks |> Seq.toArray |> Task.WaitAll
// We should never get to this point
printfn "WaitAll() has not thrown exceptions. THIS WAS NOT EXPECTED."
with :? AggregateException as e ->
printfn "\nThe following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)"
for ex in e.InnerExceptions do
printfn $"\n-------------------------------------------------\n{ex}"
// 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()
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks
Module WaitAllDemo
Sub Main()
Dim tasks As New List(Of Task(Of Integer))()
' Define a delegate that prints and returns the system tick count
Dim action As Func(Of Object, Integer) = Function(obj As Object)
Dim i As Integer = CInt(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 AndAlso i <= 5 Then
Throw New InvalidOperationException("SIMULATED EXCEPTION")
End If
Dim tickCount As Integer = Environment.TickCount
Console.WriteLine("Task={0}, i={1}, TickCount={2}, Thread={3}", Task.CurrentId, i, tickCount, Thread.CurrentThread.ManagedThreadId)
Return tickCount
End Function
' Construct started tasks
For i As Integer = 0 To 9
Dim index As Integer = i
tasks.Add(Task(Of Integer).Factory.StartNew(action, index))
Next
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 e As AggregateException
Console.WriteLine(vbLf & "The following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)")
For j As Integer = 0 To e.InnerExceptions.Count - 1
Console.WriteLine(vbLf & "-------------------------------------------------" & vbLf & "{0}", e.InnerExceptions(j).ToString())
Next
End Try
End Sub
End Module
' 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()
适用于
WaitAll(Task[], Int32)
等待提供 Task 的所有对象在指定的毫秒数内完成执行。
public:
static bool WaitAll(cli::array <System::Threading::Tasks::Task ^> ^ tasks, int millisecondsTimeout);
public static bool WaitAll(System.Threading.Tasks.Task[] tasks, int millisecondsTimeout);
static member WaitAll : System.Threading.Tasks.Task[] * int -> bool
Public Shared Function WaitAll (tasks As Task(), millisecondsTimeout As Integer) As Boolean
参数
返回
true 如果所有 Task 实例在分配的时间内完成执行,则为 ;否则为 false。
例外
已释放其中的tasks一个或多个Task对象。
参数 tasks 为 null.
至少取消了其中一个 Task 实例。 如果任务已取消,则AggregateException包含其InnerExceptions集合中的一个OperationCanceledException。
-或-
在执行至少一个 Task 实例的过程中引发了异常。
millisecondsTimeout 是非 -1 的负数,表示无限超时。
该 tasks 参数包含 null 元素。
适用于
WaitAll(Task[], CancellationToken)
等待所有提供 Task 的对象完成执行,除非取消等待。
public:
static void WaitAll(cli::array <System::Threading::Tasks::Task ^> ^ tasks, System::Threading::CancellationToken cancellationToken);
public static void WaitAll(System.Threading.Tasks.Task[] tasks, System.Threading.CancellationToken cancellationToken);
static member WaitAll : System.Threading.Tasks.Task[] * System.Threading.CancellationToken -> unit
Public Shared Sub WaitAll (tasks As Task(), cancellationToken As CancellationToken)
参数
- cancellationToken
- CancellationToken
等待任务完成时观察的 A CancellationToken 。
例外
已取消 cancellationToken。
参数 tasks 为 null.
至少取消了其中一个 Task 实例。 如果任务已取消,则AggregateException包含其InnerExceptions集合中的一个OperationCanceledException。
-或-
在执行至少一个 Task 实例的过程中引发了异常。
该 tasks 参数包含 null 元素。
已释放其中的tasks一个或多个Task对象。
注解
该 cancellationToken 参数用于取消等待操作。 取消任务是一项不同的操作,由 AggregateException 上述说明发出信号。
适用于
WaitAll(Task[], TimeSpan)
等待提供的所有可 Task 取消对象在指定的时间间隔内完成执行。
public:
static bool WaitAll(cli::array <System::Threading::Tasks::Task ^> ^ tasks, TimeSpan timeout);
public static bool WaitAll(System.Threading.Tasks.Task[] tasks, TimeSpan timeout);
static member WaitAll : System.Threading.Tasks.Task[] * TimeSpan -> bool
Public Shared Function WaitAll (tasks As Task(), timeout As TimeSpan) As Boolean
参数
返回
true 如果所有 Task 实例在分配的时间内完成执行,则为 ;否则为 false。
例外
已释放其中的tasks一个或多个Task对象。
参数 tasks 为 null.
至少取消了其中一个 Task 实例。 如果任务已取消,则AggregateException包含其InnerExceptions集合中的一个OperationCanceledException。
-或-
在执行至少一个 Task 实例的过程中引发了异常。
该 tasks 参数包含 null 元素。
适用于
WaitAll(Task[], Int32, CancellationToken)
等待所有提供 Task 的对象在指定的毫秒内完成执行,或直到取消等待为止。
public:
static bool WaitAll(cli::array <System::Threading::Tasks::Task ^> ^ tasks, int millisecondsTimeout, System::Threading::CancellationToken cancellationToken);
public static bool WaitAll(System.Threading.Tasks.Task[] tasks, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
static member WaitAll : System.Threading.Tasks.Task[] * int * System.Threading.CancellationToken -> bool
Public Shared Function WaitAll (tasks As Task(), millisecondsTimeout As Integer, cancellationToken As CancellationToken) As Boolean
参数
- cancellationToken
- CancellationToken
等待任务完成时观察的 A CancellationToken 。
返回
true 如果所有 Task 实例在分配的时间内完成执行,则为 ;否则为 false。
例外
已释放其中的tasks一个或多个Task对象。
参数 tasks 为 null.
至少取消了其中一个 Task 实例。 如果任务已取消,则AggregateException包含其InnerExceptions集合中的一个OperationCanceledException。
-或-
在执行至少一个 Task 实例的过程中引发了异常。
millisecondsTimeout 是非 -1 的负数,表示无限超时。
该 tasks 参数包含 null 元素。
已取消 cancellationToken。
注解
该 cancellationToken 参数用于取消等待操作。 取消任务是一项不同的操作,由 AggregateException 上述说明发出信号。