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.
public:
generic <typename TResult>
System::Threading::Tasks::Task<TResult> ^ ContinueWith(Func<System::Threading::Tasks::Task ^, TResult> ^ continuationFunction, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskContinuationOptions continuationOptions, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task<TResult> ContinueWith<TResult>(Func<System.Threading.Tasks.Task,TResult> continuationFunction, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);
member this.ContinueWith : Func<System.Threading.Tasks.Task, 'Result> * System.Threading.CancellationToken * System.Threading.Tasks.TaskContinuationOptions * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task<'Result>
Public Function ContinueWith(Of TResult) (continuationFunction As Func(Of Task, TResult), cancellationToken As CancellationToken, continuationOptions As TaskContinuationOptions, scheduler As TaskScheduler) As Task(Of TResult)
Type Parameters
The type of the result produced by the continuation.
ParametersA function to run according to the specified continuationOptions.
When run, the delegate will be passed the completed task as an argument.
A new continuation Task<TResult>.
ExceptionsThe continuationFunction
argument is null.
-or-
The scheduler
argument is null.
The following example shows how to use the ContinueWith method with continuation options:
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/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();
}
}
open System
open System.Threading
open System.Threading.Tasks
// 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/library/system.threading.tasks.taskcontinuationoptions(VS.100).aspx
let success =
fun () ->
printfn $"Task={Task.CurrentId}, Thread={Thread.CurrentThread.ManagedThreadId}: Begin successful transaction"
let failure =
fun () ->
printfn
$"Task={Task.CurrentId}, Thread={Thread.CurrentThread.ManagedThreadId}: Begin transaction and encounter an error"
raise (InvalidOperationException "SIMULATED EXCEPTION")
let commit =
fun antecendent ->
printfn $"Task={Task.CurrentId}, Thread={Thread.CurrentThread.ManagedThreadId}: Commit transaction"
let rollback =
fun (antecendent: Task) ->
// "Observe" your antecedent's exception so as to avoid an exception
// being thrown on the finalizer thread
let unused = antecendent.Exception
printfn $"Task={Task.CurrentId}, Thread={Thread.CurrentThread.ManagedThreadId}: Rollback transaction"
// Successful transaction - Begin + Commit
printfn "Demonstrating a successful transaction"
// Initial task
// Treated as "fire-and-forget" -- any exceptions will be cleaned up in rollback continuation
let tran1 = Task.Factory.StartNew success
// The following task gets scheduled only if tran1 completes successfully
let commitTran1 =
tran1.ContinueWith(commit, TaskContinuationOptions.OnlyOnRanToCompletion)
// The following task gets scheduled only if tran1 DOES NOT complete successfully
let rollbackTran1 =
tran1.ContinueWith(rollback, TaskContinuationOptions.NotOnRanToCompletion)
// For demo purposes, wait for the sample to complete
commitTran1.Wait()
// -----------------------------------------------------------------------------------
// Failed transaction - Begin + exception + Rollback
printfn "\nDemonstrating a failed transaction"
// Initial task
// Treated as "fire-and-forget" -- any exceptions will be cleaned up in rollback continuation
let tran2: Task = Task.Factory.StartNew failure
// The following task gets scheduled only if tran2 completes successfully
let commitTran2 =
tran2.ContinueWith(Action<Task> commit, TaskContinuationOptions.OnlyOnRanToCompletion)
// The following task gets scheduled only if tran2 DOES NOT complete successfully
let rollbackTran2 =
tran2.ContinueWith(Action<Task> rollback, TaskContinuationOptions.NotOnRanToCompletion)
// For demo purposes, wait for the sample to complete
rollbackTran2.Wait()
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/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
Remarks
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4