Task.WaitAll Method (Task())

Waits for all of the provided Task objects to complete execution.

Namespace:  System.Threading.Tasks
Assembly:  mscorlib (in mscorlib.dll)

Public Shared Sub WaitAll ( _
	ParamArray tasks As Task() _


Type: System.Threading.Tasks.Task()

An array of Task instances on which to wait.


One or more of the Task objects in tasks has been disposed.


The tasks argument is Nothing.


The tasks argument contains a null element.


At least one of the Task instances was canceled -or- an exception was thrown during the execution of at least one of the Task instances. If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.

The following example demonstrates using WaitAll to wait for a set of tasks to complete.

Imports System.Threading
Imports System.Threading.Tasks

Module WaitAllDemo
    ' Demonstrated features: 
    '   Task.Factory 
    '   Task.Result 
    '   Task group manipulation 
    '   Exception handling 
    ' Expected results: 
    '   10 tasks are started, each passed an index as a state object. 
    '   The tasks that receive an argument between 2 and 5 throw exceptions. 
    '   Task.WaitAll() wraps all exceptions in an AggregateException and propagates it to the main thread. 
    ' Documentation: 
    '   http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory_members(VS.100).aspx 
    Sub Main()
        ' 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 

        Const n As Integer = 10

        ' Construct started tasks 
        Dim tasks As Task(Of Integer)() = New Task(Of Integer)(n - 1) {}
        For i As Integer = 0 To n - 1
            tasks(i) = Task(Of Integer).Factory.StartNew(action, i)

        ' Exceptions thrown by tasks will be propagated to the main thread 
        ' while it waits for the tasks. The actual exceptions will be wrapped in AggregateException. 
            ' Wait for all the tasks to finish.

            ' 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())
        End Try 
    End Sub 

End Module

.NET Framework

Supported in: 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft