Task.WhenAll Method (IEnumerable(Of Task))

.NET Framework (current version)
 

Creates a task that will complete when all of the Task objects in an enumerable collection have completed.

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

Public Shared Function WhenAll (
	tasks As IEnumerable(Of Task)
) As Task

Parameters

tasks
Type: System.Collections.Generic.IEnumerable(Of Task)

The tasks to wait on for completion.

Return Value

Type: System.Threading.Tasks.Task

A task that represents the completion of all of the supplied tasks.

Exception Condition
ArgumentNullException

The tasks argument was null.

ArgumentException

The tasks collection contained a null task.

The overloads of the WhenAll method that return a Task object are typically called when you are interested in the status of a set of tasks or in the exceptions thrown by a set of tasks.

System_CAPS_noteNote

The call to WhenAll(IEnumerable(Of Task)) method does not block the calling thread.

If any of the supplied tasks completes in a faulted state, the returned task will also complete in a TaskStatus.Faulted state, where its exceptions will contain the aggregation of the set of unwrapped exceptions from each of the supplied tasks.

If none of the supplied tasks faulted but at least one of them was canceled, the returned task will end in the TaskStatus.Canceled state.

If none of the tasks faulted and none of the tasks were canceled, the resulting task will end in the TaskStatus.RanToCompletion state.

If the supplied array/enumerable contains no tasks, the returned task will immediately transition to a TaskStatus.RanToCompletion state before it's returned to the caller.

The following example creates a set of tasks that ping the URLs in an array. The tasks are stored in a List<Task> collection that is passed to the WhenAll(IEnumerable(Of Task)) method. After the call to the Wait method ensures that all threads have completed, the example examines the Task.Status property to determine whether any tasks have faulted.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: