Task.WaitAll Methode
Definition
Überlädt
WaitAll(Task[]) |
Wartet, bis alle bereitgestellten Task-Objekte die Ausführung abschließen.Waits for all of the provided Task objects to complete execution. |
WaitAll(Task[], Int32) |
Wartet darauf, dass alle bereitgestellten Task-Objekte innerhalb einer angegebenen Anzahl an Millisekunden vollständig ausgeführt werden.Waits for all of the provided Task objects to complete execution within a specified number of milliseconds. |
WaitAll(Task[], CancellationToken) |
Wartet, bis alle bereitgestellten Task-Objekte die Ausführung abschließen oder bis der Wartevorgang abgebrochen wird.Waits for all of the provided Task objects to complete execution unless the wait is cancelled. |
WaitAll(Task[], TimeSpan) |
Wartet darauf, dass alle bereitgestellten Task-Objekte, die abgebrochen werden können, innerhalb eines angegebenen Zeitintervalls vollständig ausgeführt werden.Waits for all of the provided cancellable Task objects to complete execution within a specified time interval. |
WaitAll(Task[], Int32, CancellationToken) |
Wartet darauf, dass alle bereitgestellten Task-Objekte innerhalb einer angegebenen Anzahl an Millisekunden oder vollständig ausgeführt werden, oder bis zum Abbruch des Wartevorgangs.Waits for all of the provided Task objects to complete execution within a specified number of milliseconds or until the wait is cancelled. |
WaitAll(Task[])
Wartet, bis alle bereitgestellten Task-Objekte die Ausführung abschließen.Waits for all of the provided Task objects to complete execution.
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())
Parameter
- tasks
- Task[]
Ein Array von Task-Instanzen, auf die gewartet werden soll.An array of Task instances on which to wait.
Ausnahmen
Mindestens eines der Task-Objekte in tasks
wurde verworfen.One or more of the Task objects in tasks
has been disposed.
Das tasks
-Argument lautet null
.The tasks
argument is null
.
Das tasks
-Argument enthält ein NULL-Element.The tasks
argument contains a null element.
Mindestens eine der Task-Instanzen wurde abgebrochen.At least one of the Task instances was canceled. Wenn ein Task abgebrochen wurde, enthält die AggregateException-Ausnahme eine OperationCanceledException-Ausnahme in ihrer InnerExceptions-Auflistung.If a task was canceled, the AggregateException exception contains an OperationCanceledException exception in its InnerExceptions collection.
- oder --or- Während der Ausführung mindestens einer der Task-Instanzen wurde eine Ausnahme ausgelöst.An exception was thrown during the execution of at least one of the Task instances.
Beispiele
Im folgenden Beispiel werden 10 Aufgaben gestartet, von denen jeder als-Status Objekt an einen Index übermittelt wird.The following example starts 10 tasks, each of which is passed an index as a state object. Tasks mit einem Index zwischen zwei und fünf Throw-Ausnahmen.Tasks with an index from two to five throw exceptions. Durch den Aufruf der WaitAll-Methode werden alle Ausnahmen in einem AggregateException-Objekt umschlossen und an den aufrufenden Thread weitergegeben.The call to the WaitAll method wraps all exceptions in an AggregateException object and propagates it to the calling thread.
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()
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)
Wartet darauf, dass alle bereitgestellten Task-Objekte innerhalb einer angegebenen Anzahl an Millisekunden vollständig ausgeführt werden.Waits for all of the provided Task objects to complete execution within a specified number of milliseconds.
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
Parameter
- tasks
- Task[]
Ein Array von Task-Instanzen, auf die gewartet werden soll.An array of Task instances on which to wait.
- millisecondsTimeout
- Int32
Die Anzahl von Millisekunden, die gewartet wird, oder Infinite (-1) für Warten ohne Timeout.The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.
Gibt zurück
true
wenn alle Task-Instanzen die Ausführung innerhalb der zugewiesenen Zeit abgeschlossen haben, andernfalls false
.true
if all of the Task instances completed execution within the allotted time; otherwise, false
.
Ausnahmen
Mindestens eines der Task-Objekte in tasks
wurde verworfen.One or more of the Task objects in tasks
has been disposed.
Das tasks
-Argument lautet null
.The tasks
argument is null
.
Mindestens eine der Task-Instanzen wurde abgebrochen.At least one of the Task instances was canceled. Wenn ein Task abgebrochen wurde, enthält die AggregateException eine OperationCanceledException in ihrer InnerExceptions-Auflistung.If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.
- oder --or- Während der Ausführung mindestens einer der Task-Instanzen wurde eine Ausnahme ausgelöst.An exception was thrown during the execution of at least one of the Task instances.
millisecondsTimeout
ist eine negative Zahl, jedoch nicht -1, was einen unbeschränkten Timeout darstellt.millisecondsTimeout
is a negative number other than -1, which represents an infinite time-out.
Das tasks
-Argument enthält ein NULL-Element.The tasks
argument contains a null element.
WaitAll(Task[], CancellationToken)
Wartet, bis alle bereitgestellten Task-Objekte die Ausführung abschließen oder bis der Wartevorgang abgebrochen wird.Waits for all of the provided Task objects to complete execution unless the wait is cancelled.
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
Parameter
- tasks
- Task[]
Ein Array von Task-Instanzen, auf die gewartet werden soll.An array of Task instances on which to wait.
- cancellationToken
- CancellationToken
Ein CancellationToken, das beim Warten auf den Abschluss der Aufgaben überwacht werden soll.A CancellationToken to observe while waiting for the tasks to complete.
Ausnahmen
Das cancellationToken
wurde abgebrochen.The cancellationToken
was canceled.
Das tasks
-Argument lautet null
.The tasks
argument is null
.
Mindestens eine der Task-Instanzen wurde abgebrochen.At least one of the Task instances was canceled. Wenn ein Task abgebrochen wurde, enthält die AggregateException eine OperationCanceledException in ihrer InnerExceptions-Auflistung.If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.
- oder --or- Während der Ausführung mindestens einer der Task-Instanzen wurde eine Ausnahme ausgelöst.An exception was thrown during the execution of at least one of the Task instances.
Das tasks
-Argument enthält ein NULL-Element.The tasks
argument contains a null element.
Mindestens eines der Task-Objekte in tasks
wurde verworfen.One or more of the Task objects in tasks
has been disposed.
Hinweise
Das cancellationToken
-Argument wird verwendet, um den warte Vorgang abzubrechen.The cancellationToken
argument is used to cancel the wait operation. Der Abbruch der Tasks ist ein eindeutiger Vorgang, der durch die AggregateException signalisiert wird, wie oben angegeben.Cancellation of the tasks is a distinct operation, and is signaled by the AggregateException as noted above.
WaitAll(Task[], TimeSpan)
Wartet darauf, dass alle bereitgestellten Task-Objekte, die abgebrochen werden können, innerhalb eines angegebenen Zeitintervalls vollständig ausgeführt werden.Waits for all of the provided cancellable Task objects to complete execution within a specified time interval.
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
Parameter
- tasks
- Task[]
Ein Array von Task-Instanzen, auf die gewartet werden soll.An array of Task instances on which to wait.
- timeout
- TimeSpan
Eine TimeSpan-Struktur, die die Anzahl der zu wartenden Millisekunden angibt, oder eine TimeSpan-Struktur, die -1 Millisekunden zum unendlichen Warten angibt.A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.
Gibt zurück
true
wenn alle Task-Instanzen die Ausführung innerhalb der zugewiesenen Zeit abgeschlossen haben, andernfalls false
.true
if all of the Task instances completed execution within the allotted time; otherwise, false
.
Ausnahmen
Mindestens eines der Task-Objekte in tasks
wurde verworfen.One or more of the Task objects in tasks
has been disposed.
Das tasks
-Argument lautet null
.The tasks
argument is null
.
Mindestens eine der Task-Instanzen wurde abgebrochen.At least one of the Task instances was canceled. Wenn ein Task abgebrochen wurde, enthält die AggregateException eine OperationCanceledException in ihrer InnerExceptions-Auflistung.If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.
- oder --or- Während der Ausführung mindestens einer der Task-Instanzen wurde eine Ausnahme ausgelöst.An exception was thrown during the execution of at least one of the Task instances.
timeout
ist eine negative Zahl ungleich -1 Millisekunden, die ein unendliches Timeout darstellt.timeout
is a negative number other than -1 milliseconds, which represents an infinite time-out.
- oder --or-
timeout
ist größer als MaxValue.timeout
is greater than MaxValue.
Das tasks
-Argument enthält ein NULL-Element.The tasks
argument contains a null element.
WaitAll(Task[], Int32, CancellationToken)
Wartet darauf, dass alle bereitgestellten Task-Objekte innerhalb einer angegebenen Anzahl an Millisekunden oder vollständig ausgeführt werden, oder bis zum Abbruch des Wartevorgangs.Waits for all of the provided Task objects to complete execution within a specified number of milliseconds or until the wait is cancelled.
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
Parameter
- tasks
- Task[]
Ein Array von Task-Instanzen, auf die gewartet werden soll.An array of Task instances on which to wait.
- millisecondsTimeout
- Int32
Die Anzahl von Millisekunden, die gewartet wird, oder Infinite (-1) für Warten ohne Timeout.The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.
- cancellationToken
- CancellationToken
Ein CancellationToken, das beim Warten auf den Abschluss der Aufgaben überwacht werden soll.A CancellationToken to observe while waiting for the tasks to complete.
Gibt zurück
true
wenn alle Task-Instanzen die Ausführung innerhalb der zugewiesenen Zeit abgeschlossen haben, andernfalls false
.true
if all of the Task instances completed execution within the allotted time; otherwise, false
.
Ausnahmen
Mindestens eines der Task-Objekte in tasks
wurde verworfen.One or more of the Task objects in tasks
has been disposed.
Das tasks
-Argument lautet null
.The tasks
argument is null
.
Mindestens eine der Task-Instanzen wurde abgebrochen.At least one of the Task instances was canceled. Wenn ein Task abgebrochen wurde, enthält die AggregateException eine OperationCanceledException in ihrer InnerExceptions-Auflistung.If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.
- oder --or- Während der Ausführung mindestens einer der Task-Instanzen wurde eine Ausnahme ausgelöst.An exception was thrown during the execution of at least one of the Task instances.
millisecondsTimeout
ist eine negative Zahl, jedoch nicht -1, was einen unbeschränkten Timeout darstellt.millisecondsTimeout
is a negative number other than -1, which represents an infinite time-out.
Das tasks
-Argument enthält ein NULL-Element.The tasks
argument contains a null element.
Das cancellationToken
wurde abgebrochen.The cancellationToken
was canceled.
Hinweise
Das cancellationToken
-Argument wird verwendet, um den warte Vorgang abzubrechen.The cancellationToken
argument is used to cancel the wait operation. Der Abbruch der Tasks ist ein eindeutiger Vorgang und wird durch die oben beschriebene AggregateException signalisiert.Cancellation of the tasks is a distinct operation, and is signaled by the AggregateException noted above.