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

.NET Framework (current version)
 

Creates a continuation that that is passed state information and that executes when the target Task<TResult> completes.

Namespace:   System.Threading.Tasks
Assembly:  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.

Exception Condition
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();
      cts.Dispose();
   }
}
// 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

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