Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

Task::ContinueWith<TResult> Method (Func<Task, TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)

Creates a continuation that executes according to the specified continuation options and returns a value. The continuation is passed a cancellation token and uses a specified scheduler.

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

public:
generic<typename TResult>
Task<TResult>^ ContinueWith(
	Func<Task^, TResult>^ continuationFunction, 
	CancellationToken cancellationToken, 
	TaskContinuationOptions continuationOptions, 
	TaskScheduler^ scheduler
)

Type Parameters

TResult

The type of the result produced by the continuation.

Parameters

continuationFunction
Type: System::Func<Task, TResult>

A function to run according to the specified continuationOptions. When run, the delegate will be passed the completed task as an argument.

cancellationToken
Type: System.Threading::CancellationToken

The CancellationToken that will be assigned to the new continuation task.

continuationOptions
Type: System.Threading.Tasks::TaskContinuationOptions

Options for when the continuation is scheduled and how it behaves. This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.

scheduler
Type: System.Threading.Tasks::TaskScheduler

The TaskScheduler to associate with the continuation task and to use for its execution.

Return Value

Type: System.Threading.Tasks::Task<TResult>
A new continuation Task<TResult>.

ExceptionCondition
ObjectDisposedException

The Task has been disposed.

-or-

The CancellationTokenSource that created the token has already been disposed.

ArgumentNullException

The continuationFunction argument is null.

-or-

The scheduler argument is null.

ArgumentOutOfRangeException

The continuationOptions argument specifies an invalid value for TaskContinuationOptions.

The returned Task<TResult> will not be scheduled for execution until the current task has completed. If the criteria specified through the continuationOptions parameter are not met, the continuation task will be canceled instead of scheduled.

The following example shows how to use the ContinueWith method with continuation options:

Imports System.Threading
Imports System.Threading.Tasks
Module ContuationOptionsDemo
    ' Demonstrated features: 
    '   TaskContinuationOptions 
    '   Task.ContinueWith() 
    '   Task.Factory 
    '   Task.Wait() 
    ' Expected results: 
    '   This sample demonstrates branched continuation sequences - Task+Commit or Task+Rollback. 
    '   Notice that no if statements are used. 
    '   The first sequence is successful - tran1 and commitTran1 are executed. rollbackTran1 is canceled. 
    '   The second sequence is unsuccessful - tran2 and rollbackTran2 are executed. tran2 is faulted, and commitTran2 is canceled. 
    ' Documentation: 
    '   http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskcontinuationoptions(VS.100).aspx 
    Private Sub Main()
        Dim success As Action = Sub()
                                    Console.WriteLine("Task={0}, Thread={1}: Begin successful transaction", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
                                End Sub 

        Dim failure As Action = Sub()
                                    Console.WriteLine("Task={0}, Thread={1}: Begin transaction and encounter an error", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
                                    Throw New InvalidOperationException("SIMULATED EXCEPTION")
                                End Sub 

        Dim commit As Action(Of Task) = Sub(antecendent)
                                            Console.WriteLine("Task={0}, Thread={1}: Commit transaction", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
                                        End Sub 

        Dim rollback As Action(Of Task) = Sub(antecendent)
                                              ' "Observe" your antecedent's exception so as to avoid an exception 
                                              ' being thrown on the finalizer thread 
                                              Dim unused = antecendent.Exception

                                              Console.WriteLine("Task={0}, Thread={1}: Rollback transaction", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
                                          End Sub 

        ' Successful transaction - Begin + Commit
        Console.WriteLine("Demonstrating a successful transaction")

        ' Initial task 
        ' Treated as "fire-and-forget" -- any exceptions will be cleaned up in rollback continuation 
        Dim tran1 As Task = Task.Factory.StartNew(success)

        ' The following task gets scheduled only if tran1 completes successfully 
        Dim commitTran1 = tran1.ContinueWith(commit, TaskContinuationOptions.OnlyOnRanToCompletion)

        ' The following task gets scheduled only if tran1 DOES NOT complete successfully 
        Dim rollbackTran1 = tran1.ContinueWith(rollback, TaskContinuationOptions.NotOnRanToCompletion)

        ' For demo purposes, wait for the sample to complete
        commitTran1.Wait()

        ' ----------------------------------------------------------------------------------- 


        ' Failed transaction - Begin + exception + Rollback 
        Console.WriteLine(vbLf & "Demonstrating a failed transaction")

        ' Initial task 
        ' Treated as "fire-and-forget" -- any exceptions will be cleaned up in rollback continuation 
        Dim tran2 As Task = Task.Factory.StartNew(failure)

        ' The following task gets scheduled only if tran2 completes successfully 
        Dim commitTran2 = tran2.ContinueWith(commit, TaskContinuationOptions.OnlyOnRanToCompletion)

        ' The following task gets scheduled only if tran2 DOES NOT complete successfully 
        Dim rollbackTran2 = tran2.ContinueWith(rollback, TaskContinuationOptions.NotOnRanToCompletion)

        ' For demo purposes, wait for the sample to complete
        rollbackTran2.Wait()
    End Sub 
End Module
No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

.NET for Windows Phone apps

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

Portable Class Library

Supported in: Portable Class Library
Show:
© 2015 Microsoft