Expand Minimize

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

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

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

public Task ContinueWith(
	Action<Task<TResult>> continuationAction,
	CancellationToken cancellationToken
)

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.

ExceptionCondition
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.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Timers = System.Timers;

public class Example
{
   static CancellationTokenSource ts;

   public static void Main(string[] args)
   {
      int upperBound = args.Length >= 1 ? Int32.Parse(args[0]) : 200;
      ts = new CancellationTokenSource();
      CancellationToken token = ts.Token;
      Timers.Timer timer = new Timers.Timer(3000);
      timer.Elapsed += TimedOutEvent;
      timer.AutoReset = false;
      timer.Enabled = true;

      var t1 = Task.Run(() => { // True = composite. 
                                // False = prime. 
                                bool[] values = new bool[upperBound + 1];
                                for (int ctr = 2; ctr <= (int) Math.Sqrt(upperBound); ctr++) {
                                   if (values[ctr] == false) {
                                      for (int product = ctr * ctr; product <= upperBound;
                                                                    product = product + ctr)
                                         values[product] = true;
                                   }
                                   token.ThrowIfCancellationRequested();
                                }
                                return values; }, token);

      var t2 = t1.ContinueWith( (antecedent) => { // Create a list of prime numbers. 
                                                  var  primes = new List<int>();
                                                  token.ThrowIfCancellationRequested();
                                                  bool[] numbers = antecedent.Result;
                                                  string output = String.Empty;

                                                  for (int ctr = 1; ctr <= numbers.GetUpperBound(0); ctr++)
                                                     if (numbers[ctr] == false)
                                                        primes.Add(ctr);

                                                  // Create the output string. 
                                                  for (int ctr = 0; ctr < primes.Count; ctr++) {
                                                     token.ThrowIfCancellationRequested();
                                                     output += primes[ctr].ToString("N0");
                                                     if (ctr < primes.Count - 1)
                                                        output += ",  ";
                                                     if ((ctr + 1) % 8 == 0)
                                                        output += Environment.NewLine;
                                                  }
                                                  //Display the result.
                                                  Console.WriteLine("Prime numbers from 1 to {0}:\n",
                                                                    upperBound);
                                                  Console.WriteLine(output);
                                                }, token);
      try {
         t2.Wait();
      }
      catch (AggregateException ae) {
         foreach (var e in ae.InnerExceptions) {
            if (e.GetType() == typeof(TaskCanceledException))
               Console.WriteLine("The operation was cancelled.");
            else
               Console.WriteLine("ELSE: {0}: {1}", e.GetType().Name, e.Message);
         }
      }
   }

   private static void TimedOutEvent(Object source, Timers.ElapsedEventArgs e)
   {
      ts.Cancel();
   }
}
// If cancellation is not requested, the example displays output like the following: 
//       Prime numbers from 1 to 400: 
// 
//       1,  2,  3,  5,  7,  11,  13,  17, 
//       19,  23,  29,  31,  37,  41,  43,  47, 
//       53,  59,  61,  67,  71,  73,  79,  83, 
//       89,  97,  101,  103,  107,  109,  113,  127, 
//       131,  137,  139,  149,  151,  157,  163,  167, 
//       173,  179,  181,  191,  193,  197,  199,  211, 
//       223,  227,  229,  233,  239,  241,  251,  257, 
//       263,  269,  271,  277,  281,  283,  293,  307, 
//       311,  313,  317,  331,  337,  347,  349,  353, 
//       359,  367,  373,  379,  383,  389,  397,  401 
// If cancellation is requested, the example displays output like the following: 
//       The operation was cancelled.

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.

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft