Expand Minimize

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

.NET Framework 4.6 and 4.5

Creates a continuation that that is passed state information and that executes 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>, Object> continuationAction,
	Object state
)

Parameters

continuationAction
Type: System.Action<Task<TResult>, Object>

An action to run when the Task<TResult> completes. When run, the delegate is passed the completed task and the caller-supplied state object as arguments.

state
Type: System.Object

An object representing data to be used by the continuation action.

Return Value

Type: System.Threading.Tasks.Task
A new continuation Task.

ExceptionCondition
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 a task that is passed an integer between 2 and 20 and returns an array that contains the first ten exponents (from n1 to n10) of that number. A continuation task is then responsible for displaying the exponents. It is passed both the antecedent and the original number whose exponents the antecedent generates.

using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      var cts = new CancellationTokenSource();
      var token = cts.Token;

      // Get an integer to generate a list of its exponents. 
      var rnd = new Random();
      var number = rnd.Next(2, 21);

      var t = Task.Factory.StartNew( (value) => { int n = (int) value;
                                                  long[] values = new long[10];
                                                  for (int ctr = 1; ctr <= 10; ctr++)
                                                     values[ctr - 1] = (long) Math.Pow(n, ctr);

                                                  return values;
                                                }, number);
      var continuation = t.ContinueWith( (antecedent, value) => { Console.WriteLine("Exponents of {0}:", value);
                                                                  for (int ctr = 0; ctr <= 9; ctr++)
                                                                     Console.WriteLine("   {0} {1} {2} = {3:N0}",
                                                                                       value, "\u02C6", ctr + 1,
                                                                                       antecedent.Result[ctr]);
                                                                  Console.WriteLine();
                                                                }, number);
      continuation.Wait();
   }
}
// The example displays output like the following: 
//       Exponents of 2: 
//          2 ^ 1 = 2 
//          2 ^ 2 = 4 
//          2 ^ 3 = 8 
//          2 ^ 4 = 16 
//          2 ^ 5 = 32 
//          2 ^ 6 = 64 
//          2 ^ 7 = 128 
//          2 ^ 8 = 256 
//          2 ^ 9 = 512 
//          2 ^ 10 = 1,024

.NET Framework

Supported in: 4.6, 4.5

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