Creates a continuation that executes according to the condition specified in continuationOptions.
Namespace: System.Threading.Tasks
Assembly: mscorlib (in mscorlib.dll)
public Task<TResult> ContinueWith<TResult>(
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.
| Exception | Condition |
|---|---|
| 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
using System; using System.Threading; using System.Threading.Tasks; class ContinuationOptionsDemo { // 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 static void Main() { Action success = () => Console.WriteLine("Task={0}, Thread={1}: Begin successful transaction", Task.CurrentId, Thread.CurrentThread.ManagedThreadId); Action failure = () => { Console.WriteLine("Task={0}, Thread={1}: Begin transaction and encounter an error", Task.CurrentId, Thread.CurrentThread.ManagedThreadId); throw new InvalidOperationException("SIMULATED EXCEPTION"); }; Action<Task> commit = (antecendent) => Console.WriteLine("Task={0}, Thread={1}: Commit transaction", Task.CurrentId, Thread.CurrentThread.ManagedThreadId); Action<Task> rollback = (antecendent) => { // "Observe" your antecedent's exception so as to avoid an exception // being thrown on the finalizer thread var unused = antecendent.Exception; Console.WriteLine("Task={0}, Thread={1}: Rollback transaction", Task.CurrentId, Thread.CurrentThread.ManagedThreadId); }; // 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 Task tran1 = Task.Factory.StartNew(success); // The following task gets scheduled only if tran1 completes successfully var commitTran1 = tran1.ContinueWith(commit, TaskContinuationOptions.OnlyOnRanToCompletion); // The following task gets scheduled only if tran1 DOES NOT complete successfully var rollbackTran1 = tran1.ContinueWith(rollback, TaskContinuationOptions.NotOnRanToCompletion); // For demo purposes, wait for the sample to complete commitTran1.Wait(); // ----------------------------------------------------------------------------------- // Failed transaction - Begin + exception + Rollback Console.WriteLine("\nDemonstrating a failed transaction"); // Initial task // Treated as "fire-and-forget" -- any exceptions will be cleaned up in rollback continuation Task tran2 = Task.Factory.StartNew(failure); // The following task gets scheduled only if tran2 completes successfully var commitTran2 = tran2.ContinueWith(commit, TaskContinuationOptions.OnlyOnRanToCompletion); // The following task gets scheduled only if tran2 DOES NOT complete successfully var rollbackTran2 = tran2.ContinueWith(rollback, TaskContinuationOptions.NotOnRanToCompletion); // For demo purposes, wait for the sample to complete rollbackTran2.Wait(); } }
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.