Expand Minimize

Task.ContinueWith(Of TResult) Method (Func(Of Task, TResult))

Creates a continuation that executes asynchronously when the target Task completes.

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

Public Function ContinueWith(Of TResult) ( _
	continuationFunction As Func(Of Task, TResult) _
) As Task(Of TResult)

Type Parameters


The type of the result produced by the continuation.


Type: System.Func(Of Task, TResult)

A function to run when the Task completes. When run, the delegate will be passed the completed task as an argument.

Return Value

Type: System.Threading.Tasks.Task(Of TResult)
A new continuation Task(Of TResult).


The Task has been disposed.


The continuationFunction argument is null.

The returned Task(Of TResult) will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting out early due to being canceled.

The following example shows how to use the ContinueWith method:

Imports System.Threading
Imports System.Threading.Tasks

Module ContinuationDemo

    ' Demonstrated features: 
    '   Task.Factory 
    '   Task.ContinueWith() 
    '   Task.Wait() 
    ' Expected results: 
    '   A sequence of three unrelated tasks is created and executed in this order - alpha, beta, gamma. 
    '   A sequence of three related tasks is created - each task negates its argument and passes is to the next task: 5, -5, 5 is printed. 
    '   A sequence of three unrelated tasks is created where tasks have different types. 
    ' Documentation: 
    '   http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory_members(VS.100).aspx 
    Sub Main()
        Dim action As Action(Of String) = Sub(str) Console.WriteLine("Task={0}, str={1}, Thread={2}", Task.CurrentId, str, Thread.CurrentThread.ManagedThreadId)

        ' Creating a sequence of action tasks (that return no result).
        Console.WriteLine("Creating a sequence of action tasks (that return no result)")
        ' Continuations ignore antecedent data
        Task.Factory.StartNew(Sub() action("alpha")).ContinueWith(Sub(antecendent) action("beta")).ContinueWith(Sub(antecendent) action("gamma")).Wait()

        Dim negate As Func(Of Integer, Integer) = Function(n)
                                                      Console.WriteLine("Task={0}, n={1}, -n={2}, Thread={3}", Task.CurrentId, n, -n, Thread.CurrentThread.ManagedThreadId)
                                                      Return -n
                                                  End Function 

        ' Creating a sequence of function tasks where each continuation uses the result from its antecendent
        Console.WriteLine(vbLf & "Creating a sequence of function tasks where each continuation uses the result from its antecendent")
        Task(Of Integer).Factory.StartNew(Function() negate(5)).ContinueWith(Function(antecendent) negate(antecendent.Result)).ContinueWith(Function(antecendent) negate(antecendent.Result)).Wait()

        ' Creating a sequence of tasks where you can mix and match the types
        Console.WriteLine(vbLf & "Creating a sequence of tasks where you can mix and match the types")
        Task(Of Integer).Factory.StartNew(Function() negate(6)).ContinueWith(Sub(antecendent) action("x")).ContinueWith(Function(antecendent) negate(7)).Wait()
    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