Task.WaitAll Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Attende il completamento dell'esecuzione di tutti gli oggetti Task forniti.
Overload
WaitAll(Task[], Int32, CancellationToken) |
Attende il completamento dell'esecuzione di tutti gli oggetti Task forniti entro un numero specificato di millisecondi o finché l'attesa non viene annullata. |
WaitAll(ReadOnlySpan<Task>) | |
WaitAll(Task[]) |
Attende il completamento dell'esecuzione di tutti gli oggetti Task forniti. |
WaitAll(IEnumerable<Task>, CancellationToken) | |
WaitAll(Task[], Int32) |
Attende il completamento dell'esecuzione di tutti gli oggetti Task forniti entro un numero specificato di millisecondi. |
WaitAll(Task[], CancellationToken) |
Attende il completamento dell'esecuzione di tutti gli oggetti Task forniti a meno che l'attesa non venga annullata. |
WaitAll(Task[], TimeSpan) |
Attende il completamento dell'esecuzione di tutti gli oggetti Task forniti annullabili entro un intervallo di tempo specificato. |
WaitAll(Task[], Int32, CancellationToken)
- Origine:
- Task.cs
- Origine:
- Task.cs
- Origine:
- Task.cs
Attende il completamento dell'esecuzione di tutti gli oggetti Task forniti entro un numero specificato di millisecondi o finché l'attesa non viene annullata.
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);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
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
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
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
Parametri
- millisecondsTimeout
- Int32
Numero di millisecondi di attesa oppure Infinite (-1) per un'attesa indefinita.
- cancellationToken
- CancellationToken
Oggetto CancellationToken da osservare durante l'attesa del completamento delle attività.
Restituisce
true
se tutte le istanze di Task hanno completato l'esecuzione nel tempo consentito; in caso contrario, false
.
- Attributi
Eccezioni
Uno o più degli oggetti Task in tasks
sono stati eliminati.
Il valore dell'argomento tasks
è null
.
Almeno una delle istanze di Task è stata annullata. Se un'attività è stata annullata, AggregateException contiene un oggetto OperationCanceledException nella relativa raccolta InnerExceptions.
-oppure-
È stata generata un'eccezione durante l'esecuzione di almeno una delle istanze di Task.
millisecondsTimeout
è un numero negativo diverso da -1, che rappresenta un timeout infinito.
L'argomento tasks
contiene un elemento null.
Il parametro cancellationToken
è stato annullato.
Commenti
L'argomento cancellationToken
viene usato per annullare l'operazione di attesa. L'annullamento delle attività è un'operazione distinta e viene segnalata dalla AggregateException nota precedente.
Si applica a
WaitAll(ReadOnlySpan<Task>)
public:
static void WaitAll(ReadOnlySpan<System::Threading::Tasks::Task ^> tasks);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void WaitAll (scoped ReadOnlySpan<System.Threading.Tasks.Task> tasks);
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member WaitAll : ReadOnlySpan<System.Threading.Tasks.Task> -> unit
Public Shared Sub WaitAll (tasks As ReadOnlySpan(Of Task))
Parametri
- tasks
- ReadOnlySpan<Task>
- Attributi
Si applica a
WaitAll(Task[])
- Origine:
- Task.cs
- Origine:
- Task.cs
- Origine:
- Task.cs
Attende il completamento dell'esecuzione di tutti gli oggetti Task forniti.
public:
static void WaitAll(... cli::array <System::Threading::Tasks::Task ^> ^ tasks);
public static void WaitAll (params System.Threading.Tasks.Task[] tasks);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void WaitAll (params System.Threading.Tasks.Task[] tasks);
static member WaitAll : System.Threading.Tasks.Task[] -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member WaitAll : System.Threading.Tasks.Task[] -> unit
Public Shared Sub WaitAll (ParamArray tasks As Task())
Parametri
- Attributi
Eccezioni
Uno o più degli oggetti Task in tasks
sono stati eliminati.
Il valore dell'argomento tasks
è null
.
L'argomento tasks
contiene un elemento null.
Almeno una delle istanze di Task è stata annullata. Se un'attività è stata annullata, l'eccezione AggregateException contiene un'eccezione OperationCanceledException nella raccolta InnerExceptions.
-oppure-
È stata generata un'eccezione durante l'esecuzione di almeno una delle istanze di Task.
Esempio
L'esempio seguente avvia 10 attività, ognuna delle quali viene passata un indice come oggetto stato. Le attività con un indice da due a cinque generano eccezioni. La chiamata al WaitAll metodo esegue il wrapping di tutte le eccezioni in un AggregateException oggetto e la propaga al thread chiamante.
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()
Si applica a
WaitAll(IEnumerable<Task>, CancellationToken)
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void WaitAll (System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task> tasks, System.Threading.CancellationToken cancellationToken = default);
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member WaitAll : seq<System.Threading.Tasks.Task> * System.Threading.CancellationToken -> unit
Public Shared Sub WaitAll (tasks As IEnumerable(Of Task), Optional cancellationToken As CancellationToken = Nothing)
Parametri
- tasks
- IEnumerable<Task>
- cancellationToken
- CancellationToken
- Attributi
Si applica a
WaitAll(Task[], Int32)
- Origine:
- Task.cs
- Origine:
- Task.cs
- Origine:
- Task.cs
Attende il completamento dell'esecuzione di tutti gli oggetti Task forniti entro un numero specificato di millisecondi.
public:
static bool WaitAll(cli::array <System::Threading::Tasks::Task ^> ^ tasks, int millisecondsTimeout);
public static bool WaitAll (System.Threading.Tasks.Task[] tasks, int millisecondsTimeout);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool WaitAll (System.Threading.Tasks.Task[] tasks, int millisecondsTimeout);
static member WaitAll : System.Threading.Tasks.Task[] * int -> bool
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member WaitAll : System.Threading.Tasks.Task[] * int -> bool
Public Shared Function WaitAll (tasks As Task(), millisecondsTimeout As Integer) As Boolean
Parametri
- millisecondsTimeout
- Int32
Numero di millisecondi di attesa oppure Infinite (-1) per un'attesa indefinita.
Restituisce
true
se tutte le istanze di Task hanno completato l'esecuzione nel tempo consentito; in caso contrario, false
.
- Attributi
Eccezioni
Uno o più degli oggetti Task in tasks
sono stati eliminati.
Il valore dell'argomento tasks
è null
.
Almeno una delle istanze di Task è stata annullata. Se un'attività è stata annullata, AggregateException contiene un oggetto OperationCanceledException nella relativa raccolta InnerExceptions.
-oppure-
È stata generata un'eccezione durante l'esecuzione di almeno una delle istanze di Task.
millisecondsTimeout
è un numero negativo diverso da -1, che rappresenta un timeout infinito.
L'argomento tasks
contiene un elemento null.
Si applica a
WaitAll(Task[], CancellationToken)
- Origine:
- Task.cs
- Origine:
- Task.cs
- Origine:
- Task.cs
Attende il completamento dell'esecuzione di tutti gli oggetti Task forniti a meno che l'attesa non venga annullata.
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);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void WaitAll (System.Threading.Tasks.Task[] tasks, System.Threading.CancellationToken cancellationToken);
static member WaitAll : System.Threading.Tasks.Task[] * System.Threading.CancellationToken -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member WaitAll : System.Threading.Tasks.Task[] * System.Threading.CancellationToken -> unit
Public Shared Sub WaitAll (tasks As Task(), cancellationToken As CancellationToken)
Parametri
- cancellationToken
- CancellationToken
Oggetto CancellationToken da osservare durante l'attesa del completamento delle attività.
- Attributi
Eccezioni
Il parametro cancellationToken
è stato annullato.
Il valore dell'argomento tasks
è null
.
Almeno una delle istanze di Task è stata annullata. Se un'attività è stata annullata, AggregateException contiene un oggetto OperationCanceledException nella relativa raccolta InnerExceptions.
-oppure-
È stata generata un'eccezione durante l'esecuzione di almeno una delle istanze di Task.
L'argomento tasks
contiene un elemento null.
Uno o più degli oggetti Task in tasks
sono stati eliminati.
Commenti
L'argomento cancellationToken
viene usato per annullare l'operazione di attesa. L'annullamento delle attività è un'operazione distinta e viene segnalato dall'oggetto AggregateException indicato in precedenza.
Si applica a
WaitAll(Task[], TimeSpan)
- Origine:
- Task.cs
- Origine:
- Task.cs
- Origine:
- Task.cs
Attende il completamento dell'esecuzione di tutti gli oggetti Task forniti annullabili entro un intervallo di tempo specificato.
public:
static bool WaitAll(cli::array <System::Threading::Tasks::Task ^> ^ tasks, TimeSpan timeout);
public static bool WaitAll (System.Threading.Tasks.Task[] tasks, TimeSpan timeout);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool WaitAll (System.Threading.Tasks.Task[] tasks, TimeSpan timeout);
static member WaitAll : System.Threading.Tasks.Task[] * TimeSpan -> bool
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member WaitAll : System.Threading.Tasks.Task[] * TimeSpan -> bool
Public Shared Function WaitAll (tasks As Task(), timeout As TimeSpan) As Boolean
Parametri
- timeout
- TimeSpan
Oggetto TimeSpan che rappresenta il numero di millisecondi di attesa oppure TimeSpan che rappresenta -1 millisecondi per un'attesa indefinita.
Restituisce
true
se tutte le istanze di Task hanno completato l'esecuzione nel tempo consentito; in caso contrario, false
.
- Attributi
Eccezioni
Uno o più degli oggetti Task in tasks
sono stati eliminati.
Il valore dell'argomento tasks
è null
.
Almeno una delle istanze di Task è stata annullata. Se un'attività è stata annullata, AggregateException contiene un oggetto OperationCanceledException nella relativa raccolta InnerExceptions.
-oppure-
È stata generata un'eccezione durante l'esecuzione di almeno una delle istanze di Task.
timeout
è un numero negativo diverso da -1 millisecondi, che rappresenta un timeout infinito.
-oppure-
timeout
è maggiore di Int32.MaxValue.
L'argomento tasks
contiene un elemento null.
Si applica a
Commenti e suggerimenti
https://aka.ms/ContentUserFeedback.
Presto disponibile: Nel corso del 2024 verranno gradualmente disattivati i problemi di GitHub come meccanismo di feedback per il contenuto e ciò verrà sostituito con un nuovo sistema di feedback. Per altre informazioni, vedereInvia e visualizza il feedback per