Task<TResult>.ContinueWith Method

Definition

Creates a continuation task that executes when another task completes.

Overloads

ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)

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

ContinueWith(Action<Task<TResult>,Object>, Object, TaskScheduler)

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

ContinueWith(Action<Task<TResult>,Object>, Object, TaskContinuationOptions)

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

ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken)

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

ContinueWith(Action<Task<TResult>>, CancellationToken, TaskContinuationOptions, TaskScheduler)

Creates a continuation that executes according the condition specified in continuationOptions.

ContinueWith(Action<Task<TResult>>, TaskContinuationOptions)

Creates a continuation that executes according the condition specified in continuationOptions.

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

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

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

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

ContinueWith(Action<Task<TResult>>)

Creates a continuation that executes asynchronously when the target task completes.

ContinueWith(Action<Task<TResult>>, TaskScheduler)

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

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)

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

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)

Creates a continuation that executes according the condition specified in continuationOptions.

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskScheduler)

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

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken)

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

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskContinuationOptions)

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

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskContinuationOptions)

Creates a continuation that executes according the condition specified in continuationOptions.

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken)

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

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object)

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

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>)

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

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskScheduler)

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

ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);
C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object?> continuationAction, object? state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);

Parameters

continuationAction
Action<Task<TResult>,Object>

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

state
Object

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

cancellationToken
CancellationToken

The CancellationToken that will be assigned to the new continuation task.

continuationOptions
TaskContinuationOptions

Options for when the continuation is scheduled and how it behaves. This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.

scheduler
TaskScheduler

The TaskScheduler to associate with the continuation task and to use for its execution.

Returns

A new continuation Task.

Exceptions

The scheduler argument is null.

The continuationOptions argument specifies an invalid value for TaskContinuationOptions.

The provided CancellationToken has already been disposed.

Remarks

The returned Task 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.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith(Action<Task<TResult>,Object>, Object, TaskScheduler)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state, System.Threading.Tasks.TaskScheduler scheduler);
C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object?> continuationAction, object? state, System.Threading.Tasks.TaskScheduler scheduler);

Parameters

continuationAction
Action<Task<TResult>,Object>

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

state
Object

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

scheduler
TaskScheduler

The TaskScheduler to associate with the continuation task and to use for its execution.

Returns

A new continuation Task.

Exceptions

The scheduler argument is null.

Remarks

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.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith(Action<Task<TResult>,Object>, Object, TaskContinuationOptions)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object?> continuationAction, object? state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);

Parameters

continuationAction
Action<Task<TResult>,Object>

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

state
Object

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

continuationOptions
TaskContinuationOptions

Options for when the continuation is scheduled and how it behaves. This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.

Returns

A new continuation Task.

Exceptions

The continuationAction argument is null.

The continuationOptions argument specifies an invalid value for TaskContinuationOptions.

Remarks

The returned Task will not be scheduled for execution until the current task has completed. If the continuation criteria specified through the continuationOptions parameter are not met, the continuation task will be canceled instead of scheduled.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state, System.Threading.CancellationToken cancellationToken);
C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object?> continuationAction, object? state, System.Threading.CancellationToken cancellationToken);

Parameters

continuationAction
Action<Task<TResult>,Object>

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

state
Object

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

cancellationToken
CancellationToken

The CancellationToken that will be assigned to the new continuation task.

Returns

A new continuation Task.

Exceptions

The continuationAction argument is null.

The provided CancellationToken has already been disposed.

Remarks

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.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith(Action<Task<TResult>>, CancellationToken, TaskContinuationOptions, TaskScheduler)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

Creates a continuation that executes according the condition specified in continuationOptions.

C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>> continuationAction, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);

Parameters

continuationAction
Action<Task<TResult>>

An action to run according the condition specified in continuationOptions. When run, the delegate will be passed the completed task as an argument.

cancellationToken
CancellationToken

The CancellationToken that will be assigned to the new continuation task.

continuationOptions
TaskContinuationOptions

Options for when the continuation is scheduled and how it behaves. This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.

scheduler
TaskScheduler

The TaskScheduler to associate with the continuation task and to use for its execution.

Returns

A new continuation Task.

Exceptions

The Task<TResult> has been disposed.

-or-

The CancellationTokenSource that created cancellationToken has already been disposed.

The continuationAction argument is null.

-or-

The scheduler argument is null.

The continuationOptions argument specifies an invalid value for TaskContinuationOptions.

Remarks

The returned Task 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. For more information, see Chaining Tasks by Using Continuation Tasks.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith(Action<Task<TResult>>, TaskContinuationOptions)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

Creates a continuation that executes according the condition specified in continuationOptions.

C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>> continuationAction, System.Threading.Tasks.TaskContinuationOptions continuationOptions);

Parameters

continuationAction
Action<Task<TResult>>

An action to according the condition specified in continuationOptions. When run, the delegate will be passed the completed task as an argument.

continuationOptions
TaskContinuationOptions

Options for when the continuation is scheduled and how it behaves. This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.

Returns

A new continuation Task.

Exceptions

The Task<TResult> has been disposed.

The continuationAction argument is null.

The continuationOptions argument specifies an invalid value for TaskContinuationOptions.

Remarks

The returned Task will not be scheduled for execution until the current task has completed. If the continuation criteria specified through the continuationOptions parameter are not met, the continuation task will be canceled instead of scheduled.

For more information, see Chaining Tasks by Using Continuation Tasks.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>> continuationAction, System.Threading.CancellationToken cancellationToken);

Parameters

continuationAction
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
CancellationToken

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

Returns

A new continuation task.

Exceptions

The Task<TResult> has been disposed.

-or-

The CancellationTokenSource that created cancellationToken has been disposed.

The continuationAction argument is null.

Examples

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.

C#
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] ) {
                                      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] )
                                                        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);
         }
      }
      finally {
         ts.Dispose();
      }
   }

   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.

Remarks

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.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state);
C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object?> continuationAction, object? state);

Parameters

continuationAction
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
Object

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

Returns

A new continuation Task.

Exceptions

The continuationAction argument is null.

Examples

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.

C#
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

Remarks

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.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith(Action<Task<TResult>>)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

Creates a continuation that executes asynchronously when the target task completes.

C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>> continuationAction);

Parameters

continuationAction
Action<Task<TResult>>

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

Returns

A new continuation task.

Exceptions

The Task<TResult> has been disposed.

The continuationAction argument is null.

Examples

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.

C#
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main(string[] args)
   {
      int upperBound = args.Length >= 1 ? Int32.Parse(args[0]) : 200;

      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]) {
                                      for (int product = ctr * ctr; product <= upperBound;
                                                                    product = product + ctr)
                                         values[product] = true;
                                   }
                                }
                                return values; });
      var t2 = t1.ContinueWith( (antecedent) => { // Create a list of prime numbers.
                                                  var  primes = new List<int>();
                                                  bool[] numbers = antecedent.Result;
                                                  string output = String.Empty;

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

                                                  // Create the output string.
                                                  for (int ctr = 0; ctr < primes.Count; ctr++) {
                                                     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);
                                                });
      try {
         t2.Wait();
      }
      catch (AggregateException ae) {
         foreach (var e in ae.InnerExceptions)
            Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
      }
   }
}
// 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

Remarks

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 early due to being canceled.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith(Action<Task<TResult>>, TaskScheduler)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>> continuationAction, System.Threading.Tasks.TaskScheduler scheduler);

Parameters

continuationAction
Action<Task<TResult>>

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

scheduler
TaskScheduler

The TaskScheduler to associate with the continuation task and to use for its execution.

Returns

A new continuation Task.

Exceptions

The Task<TResult> has been disposed.

The continuationAction argument is null.

-or-

The scheduler argument is null.

Remarks

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.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);
C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object?,TNewResult> continuationFunction, object? state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);

Type Parameters

TNewResult

The type of the result produced by the continuation.

Parameters

continuationFunction
Func<Task<TResult>,Object,TNewResult>

A function to run when the Task<TResult> completes. When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.

state
Object

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

cancellationToken
CancellationToken

The CancellationToken that will be assigned to the new task.

continuationOptions
TaskContinuationOptions

Options for when the continuation is scheduled and how it behaves. This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.

scheduler
TaskScheduler

The TaskScheduler to associate with the continuation task and to use for its execution.

Returns

Task<TNewResult>

A new continuation Task<TResult>.

Exceptions

The scheduler argument is null.

The continuationOptions argument specifies an invalid value for TaskContinuationOptions.

The provided CancellationToken has already been disposed.

Remarks

The returned Task<TResult> 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 continuationFunction, when executed, should return a Task<TResult>. This task's completion state will be transferred to the task returned from the Task<TResult>.ContinueWith call.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

Creates a continuation that executes according the condition specified in continuationOptions.

C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);

Type Parameters

TNewResult

The type of the result produced by the continuation.

Parameters

continuationFunction
Func<Task<TResult>,TNewResult>

A function to run according the condition specified in continuationOptions.

When run, the delegate will be passed as an argument this completed task.

cancellationToken
CancellationToken

The CancellationToken that will be assigned to the new task.

continuationOptions
TaskContinuationOptions

Options for when the continuation is scheduled and how it behaves. This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.

scheduler
TaskScheduler

The TaskScheduler to associate with the continuation task and to use for its execution.

Returns

Task<TNewResult>

A new continuation Task<TResult>.

Exceptions

The Task<TResult> has been disposed.

-or-

The CancellationTokenSource that created cancellationToken has already been disposed.

The continuationFunction argument is null.

-or-

The scheduler argument is null.

The continuationOptions argument specifies an invalid value for TaskContinuationOptions.

Remarks

The returned Task<TResult> 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 continuationFunction, when executed, should return a Task<TResult>.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskScheduler)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state, System.Threading.Tasks.TaskScheduler scheduler);
C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object?,TNewResult> continuationFunction, object? state, System.Threading.Tasks.TaskScheduler scheduler);

Type Parameters

TNewResult

The type of the result produced by the continuation.

Parameters

continuationFunction
Func<Task<TResult>,Object,TNewResult>

A function to run when the Task<TResult> completes. When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.

state
Object

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

scheduler
TaskScheduler

The TaskScheduler to associate with the continuation task and to use for its execution.

Returns

Task<TNewResult>

A new continuation Task<TResult>.

Exceptions

The scheduler argument is null.

Remarks

The returned Task<TResult> 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.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state, System.Threading.CancellationToken cancellationToken);
C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object?,TNewResult> continuationFunction, object? state, System.Threading.CancellationToken cancellationToken);

Type Parameters

TNewResult

The type of the result produced by the continuation.

Parameters

continuationFunction
Func<Task<TResult>,Object,TNewResult>

A function to run when the Task<TResult> completes. When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.

state
Object

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

cancellationToken
CancellationToken

The CancellationToken that will be assigned to the new task.

Returns

Task<TNewResult>

A new continuation Task<TResult>.

Exceptions

The continuationFunction argument is null.

The provided CancellationToken has already been disposed.

Remarks

The returned Task<TResult> 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.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskContinuationOptions)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object?,TNewResult> continuationFunction, object? state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);

Type Parameters

TNewResult

The type of the result produced by the continuation.

Parameters

continuationFunction
Func<Task<TResult>,Object,TNewResult>

A function to run when the Task<TResult> completes. When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.

state
Object

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

continuationOptions
TaskContinuationOptions

Options for when the continuation is scheduled and how it behaves. This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.

Returns

Task<TNewResult>

A new continuation Task<TResult>.

Exceptions

The continuationFunction argument is null.

The continuationOptions argument specifies an invalid value for TaskContinuationOptions.

Remarks

The returned Task<TResult> 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 continuationFunction, when executed, should return a Task<TResult>. This task's completion state will be transferred to the task returned from the ContinueWith call.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskContinuationOptions)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

Creates a continuation that executes according the condition specified in continuationOptions.

C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction, System.Threading.Tasks.TaskContinuationOptions continuationOptions);

Type Parameters

TNewResult

The type of the result produced by the continuation.

Parameters

continuationFunction
Func<Task<TResult>,TNewResult>

A function to run according the condition specified in continuationOptions.

When run, the delegate will be passed the completed task as an argument.

continuationOptions
TaskContinuationOptions

Options for when the continuation is scheduled and how it behaves. This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.

Returns

Task<TNewResult>

A new continuation Task<TResult>.

Exceptions

The Task<TResult> has been disposed.

The continuationFunction argument is null.

The continuationOptions argument specifies an invalid value for TaskContinuationOptions.

Remarks

The returned Task<TResult> 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 continuationFunction, when executed, should return a Task<TResult>.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction, System.Threading.CancellationToken cancellationToken);

Type Parameters

TNewResult

The type of the result produced by the continuation.

Parameters

continuationFunction
Func<Task<TResult>,TNewResult>

A function to run when the Task<TResult> completes. When run, the delegate will be passed the completed task as an argument.

cancellationToken
CancellationToken

The CancellationToken that will be assigned to the new task.

Returns

Task<TNewResult>

A new continuation Task<TResult>.

Exceptions

The Task<TResult> has been disposed.

-or-

The CancellationTokenSource that created cancellationToken has already been disposed.

The continuationFunction argument is null.

Remarks

The returned Task<TResult> 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.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state);
C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object?,TNewResult> continuationFunction, object? state);

Type Parameters

TNewResult

The type of the result produced by the continuation.

Parameters

continuationFunction
Func<Task<TResult>,Object,TNewResult>

A function to run when the Task<TResult> completes. When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.

state
Object

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

Returns

Task<TNewResult>

A new continuation Task<TResult>.

Exceptions

The continuationFunction argument is null.

Examples

The following example creates a chain of continuation tasks. Each task provides the current time, a DateTime object, for the state argument of the ContinueWith(Action<Task,Object>, Object) method. Each DateTime value represents the time at which the continue task is created. Each task produces as its result a second DateTime value that represents the time at which the task finishes. After all tasks finish, the example displays the date and times at which each continuation task starts and finishes.

C#
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

// Demonstrates how to associate state with task continuations.
class ContinuationState
{
   // Simluates a lengthy operation and returns the time at which
   // the operation completed.
   public static DateTime DoWork()
   {
      // Simulate work by suspending the current thread
      // for two seconds.
      Thread.Sleep(2000);

      // Return the current time.
      return DateTime.Now;
   }

   static void Main(string[] args)
   {
      // Start a root task that performs work.
      Task<DateTime> t = Task<DateTime>.Run(delegate { return DoWork(); });

      // Create a chain of continuation tasks, where each task is
      // followed by another task that performs work.
      List<Task<DateTime>> continuations = new List<Task<DateTime>>();
      for (int i = 0; i < 5; i++)
      {
         // Provide the current time as the state of the continuation.
         t = t.ContinueWith(delegate { return DoWork(); }, DateTime.Now);
         continuations.Add(t);
      }

      // Wait for the last task in the chain to complete.
      t.Wait();

      // Print the creation time of each continuation (the state object)
      // and the completion time (the result of that task) to the console.
      foreach (var continuation in continuations)
      {
         DateTime start = (DateTime)continuation.AsyncState;
         DateTime end = continuation.Result;

         Console.WriteLine("Task was created at {0} and finished at {1}.",
            start.TimeOfDay, end.TimeOfDay);
      }
   }
}

/* Sample output:
Task was created at 10:56:21.1561762 and finished at 10:56:25.1672062.
Task was created at 10:56:21.1610677 and finished at 10:56:27.1707646.
Task was created at 10:56:21.1610677 and finished at 10:56:29.1743230.
Task was created at 10:56:21.1610677 and finished at 10:56:31.1779883.
Task was created at 10:56:21.1610677 and finished at 10:56:33.1837083.
*/

Remarks

The returned Task<TResult> 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.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction);

Type Parameters

TNewResult

The type of the result produced by the continuation.

Parameters

continuationFunction
Func<Task<TResult>,TNewResult>

A function to run when the Task<TResult> completes. When run, the delegate will be passed the completed task as an argument.

Returns

Task<TNewResult>

A new continuation Task<TResult>.

Exceptions

The Task<TResult> has been disposed.

The continuationFunction argument is null.

Remarks

The returned Task<TResult> 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.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskScheduler)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

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

C#
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction, System.Threading.Tasks.TaskScheduler scheduler);

Type Parameters

TNewResult

The type of the result produced by the continuation.

Parameters

continuationFunction
Func<Task<TResult>,TNewResult>

A function to run when the Task<TResult> completes. When run, the delegate will be passed the completed task as an argument.

scheduler
TaskScheduler

The TaskScheduler to associate with the continuation task and to use for its execution.

Returns

Task<TNewResult>

A new continuation Task<TResult>.

Exceptions

The Task<TResult> has been disposed.

The continuationFunction argument is null.

-or-

The scheduler argument is null.

Remarks

The returned Task<TResult> 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.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0