Task<'TResult>.ContinueWith Method (Action<Task<'TResult>>, CancellationToken)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Creates a cancelable continuation that executes asynchronously when the target Task<'TResult> completes.

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

member ContinueWith : 
        continuationAction:Action<Task<'TResult>> *
        cancellationToken:CancellationToken -> Task

Parameters

continuationAction
Type: System.Action<Task<'TResult>>

An action to run when the Task<'TResult> completes. When run, the delegate is passed the completed task as an argument.

cancellationToken
Type: System.Threading.CancellationToken

The cancellation token that is passed to the new continuation task.

Return Value

Type: System.Threading.Tasks.Task

A new continuation task.

Exception Condition
ObjectDisposedException

The Task<'TResult> has been disposed.

-or-

The CancellationTokenSource that created cancellationToken has been disposed.

ArgumentNullException

The continuationAction argument is null.

The returned Task 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 creates an antecedent task that uses the Sieve of Eratosthenes to calculate the prime numbers between 1 and a value entered by the user. An array is used to hold information about the prime numbers. The array index represents the number, and the element's value indicates whether that number is composite (its value is true) or prime (its value is false). This task is then passed to a continuation task, which is responsible for extracting the prime numbers from the integer array and displaying them.

A cancellation token is passed to both the antecedent and the continuation task. A System.Timers.Timer object is used to define a timeout value of 100 milliseconds. If the event fires, the CancellationTokenSource.Cancel method is called, and the cancellation token is used to request cancellation of the tasks.

No code example is currently available or this language may not be supported.

Typically, supplying a value of about 100,000 causes the timeout interval to expire and the Timer.Elapsed event to fire, and the cancellation request to be set.

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