Task<TResult>.ContinueWith 方法

定義

建立其他工作完成時執行的接續工作。

多載

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

建立目標 Task<TResult> 完成時執行的接續工作。

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

建立目標 Task<TResult> 完成時執行的接續工作。

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

建立目標 Task<TResult> 完成時執行的接續工作。

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

建立目標 Task<TResult> 完成時執行的接續工作。

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

建立會根據 continuationOptions 中指定的條件執行的接續。

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

建立會根據 continuationOptions 中指定的條件執行的接續。

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

建立當目標 Task<TResult> 完成時非同步執行的可取消接續。

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

建立當目標 Task<TResult> 完成時,會接收狀態資訊並執行的接續。

ContinueWith(Action<Task<TResult>>)

建立當目標工作完成時非同步執行的接續。

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

建立當目標 Task<TResult> 完成時非同步執行的接續。

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

建立目標 Task<TResult> 完成時執行的接續工作。

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

建立會根據 continuationOptions 中指定的條件執行的接續。

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

建立目標 Task<TResult> 完成時執行的接續工作。

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

建立目標 Task<TResult> 完成時執行的接續工作。

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

建立目標 Task<TResult> 完成時執行的接續工作。

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

建立會根據 continuationOptions 中指定的條件執行的接續。

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

建立當目標 Task<TResult> 完成時非同步執行的接續。

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

建立目標 Task<TResult> 完成時執行的接續工作。

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

建立當目標 Task<TResult> 完成時非同步執行的接續。

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

建立當目標 Task<TResult> 完成時非同步執行的接續。

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

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

建立目標 Task<TResult> 完成時執行的接續工作。

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^, System::Object ^> ^ continuationAction, System::Object ^ state, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskContinuationOptions continuationOptions, System::Threading::Tasks::TaskScheduler ^ scheduler);
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);
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);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>, obj> * obj * System.Threading.CancellationToken * System.Threading.Tasks.TaskContinuationOptions * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult), Object), state As Object, cancellationToken As CancellationToken, continuationOptions As TaskContinuationOptions, scheduler As TaskScheduler) As Task

參數

continuationAction
Action<Task<TResult>,Object>

Task<TResult> 完成時要執行的動作。 當執行時,完成的工作會傳遞至委派,並以呼叫端提供狀態的物件做為引數。

state
Object

物件,表示接續動作所使用的資料。

cancellationToken
CancellationToken

要指派給新接續工作的 CancellationToken

continuationOptions
TaskContinuationOptions

接續工作的排程時間及其行為方式的適用選項。 這包含準則 (如 OnlyOnCanceled) 和執行選項 (如 ExecuteSynchronously)。

scheduler
TaskScheduler

要與接續工作產生關聯且於執行時使用的 TaskScheduler

傳回

新的接續 Task

例外狀況

scheduler 引數為 null

continuationOptions 引數指定的 TaskContinuationOptions 值不正確。

提供的 CancellationToken 已經過處置。

備註

傳回 Task 的 將不會排程執行,直到目前的工作完成為止。 如果不符合透過 continuationOptions 參數指定的準則,則會取消接續工作,而不是排程工作。

另請參閱

適用於

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

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

建立目標 Task<TResult> 完成時執行的接續工作。

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^, System::Object ^> ^ continuationAction, System::Object ^ state, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state, System.Threading.Tasks.TaskScheduler scheduler);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task<TResult>,object?> continuationAction, object? state, System.Threading.Tasks.TaskScheduler scheduler);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>, obj> * obj * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult), Object), state As Object, scheduler As TaskScheduler) As Task

參數

continuationAction
Action<Task<TResult>,Object>

Task<TResult> 完成時要執行的動作。 當執行時,完成的工作會傳遞至委派,並以呼叫端提供狀態的物件做為引數。

state
Object

物件,表示接續動作所使用的資料。

scheduler
TaskScheduler

要與接續工作產生關聯且於執行時使用的 TaskScheduler

傳回

新的接續 Task

例外狀況

scheduler 引數為 null

備註

傳回 Task 的 將不會排程執行,直到目前的工作完成、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

另請參閱

適用於

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

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

建立目標 Task<TResult> 完成時執行的接續工作。

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^, System::Object ^> ^ continuationAction, System::Object ^ state, System::Threading::Tasks::TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task<TResult>,object?> continuationAction, object? state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>, obj> * obj * System.Threading.Tasks.TaskContinuationOptions -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult), Object), state As Object, continuationOptions As TaskContinuationOptions) As Task

參數

continuationAction
Action<Task<TResult>,Object>

Task<TResult> 完成時要執行的動作。 當執行時,完成的工作會傳遞至委派,並以呼叫端提供狀態的物件做為引數。

state
Object

物件,表示接續動作所使用的資料。

continuationOptions
TaskContinuationOptions

接續工作的排程時間及其行為方式的適用選項。 這包含準則 (如 OnlyOnCanceled) 和執行選項 (如 ExecuteSynchronously)。

傳回

新的接續 Task

例外狀況

continuationAction 引數為 null

continuationOptions 引數指定的 TaskContinuationOptions 值不正確。

備註

傳回 Task 的 將不會排程執行,直到目前的工作完成為止。 如果不符合透過 continuationOptions 參數指定的接續準則,則會取消接續工作,而不是排程工作。

另請參閱

適用於

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

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

建立目標 Task<TResult> 完成時執行的接續工作。

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^, System::Object ^> ^ continuationAction, System::Object ^ state, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state, System.Threading.CancellationToken cancellationToken);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task<TResult>,object?> continuationAction, object? state, System.Threading.CancellationToken cancellationToken);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>, obj> * obj * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult), Object), state As Object, cancellationToken As CancellationToken) As Task

參數

continuationAction
Action<Task<TResult>,Object>

Task<TResult> 完成時要執行的動作。 當執行時,完成的工作會傳遞至委派,並以呼叫端提供狀態的物件做為引數。

state
Object

物件,表示接續動作所使用的資料。

cancellationToken
CancellationToken

要指派給新接續工作的 CancellationToken

傳回

新的接續 Task

例外狀況

continuationAction 引數為 null

提供的 CancellationToken 已經過處置。

備註

傳回 Task 的 將不會排程執行,直到目前的工作完成、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

另請參閱

適用於

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

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

建立會根據 continuationOptions 中指定的條件執行的接續。

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);
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);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>> * System.Threading.CancellationToken * System.Threading.Tasks.TaskContinuationOptions * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult)), cancellationToken As CancellationToken, continuationOptions As TaskContinuationOptions, scheduler As TaskScheduler) As Task

參數

continuationAction
Action<Task<TResult>>

要根據 continuationOptions 中指定之條件執行的動作。 執行時,會將已完成的工作當做引數傳遞給委派。

cancellationToken
CancellationToken

要指派給新接續工作的 CancellationToken

continuationOptions
TaskContinuationOptions

接續工作的排程時間及其行為方式的適用選項。 這包含準則 (如 OnlyOnCanceled) 和執行選項 (如 ExecuteSynchronously)。

scheduler
TaskScheduler

要與接續工作產生關聯且於執行時使用的 TaskScheduler

傳回

新的接續 Task

例外狀況

Task<TResult> 已經處置。

-或-

已處置建立 cancellationTokenCancellationTokenSource

continuationAction 引數為 null

-或-

scheduler 引數為 null

continuationOptions 引數指定的 TaskContinuationOptions 值不正確。

備註

傳回 Task 的 將不會排程執行,直到目前的工作完成為止。 如果不符合透過 continuationOptions 參數指定的準則,則會取消接續工作,而不是排程工作。 如需詳細資訊,請參閱使用接續工作鏈結工作

另請參閱

適用於

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

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

建立會根據 continuationOptions 中指定的條件執行的接續。

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^> ^ continuationAction, System::Threading::Tasks::TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task<TResult>> continuationAction, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>> * System.Threading.Tasks.TaskContinuationOptions -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult)), continuationOptions As TaskContinuationOptions) As Task

參數

continuationAction
Action<Task<TResult>>

要根據 continuationOptions 中指定之條件執行的動作。 執行時,會將已完成的工作當做引數傳遞給委派。

continuationOptions
TaskContinuationOptions

接續工作的排程時間及其行為方式的適用選項。 這包含準則 (如 OnlyOnCanceled) 和執行選項 (如 ExecuteSynchronously)。

傳回

新的接續 Task

例外狀況

continuationAction 引數為 null

continuationOptions 引數指定的 TaskContinuationOptions 值不正確。

備註

傳回 Task 的 將不會排程執行,直到目前的工作完成為止。 如果不符合透過 continuationOptions 參數指定的接續準則,則會取消接續工作,而不是排程工作。

如需詳細資訊,請參閱使用接續工作鏈結工作

另請參閱

適用於

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

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

建立當目標 Task<TResult> 完成時非同步執行的可取消接續。

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^> ^ continuationAction, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task<TResult>> continuationAction, System.Threading.CancellationToken cancellationToken);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>> * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult)), cancellationToken As CancellationToken) As Task

參數

continuationAction
Action<Task<TResult>>

Task<TResult> 完成時要執行的動作。 執行時,會將已完成的工作當做引數傳遞給委派。

cancellationToken
CancellationToken

要傳遞給新接續工作的取消權杖。

傳回

新的接續工作。

例外狀況

Task<TResult> 已經處置。

-或-

已處置建立 cancellationTokenCancellationTokenSource

continuationAction 引數為 null

範例

下列範例會建立前項工作,其使用 Eratosthenes 的 Sieve 來計算介於 1 與使用者輸入的值之間的質數。 陣列用來保存質數的相關資訊。 陣列索引代表數位,而元素的值會指出該數位是複合的, (其值 true 是) 還是質數 (其值是 false) 。 此工作接著會傳遞至接續工作,負責從整數陣列擷取質數並加以顯示。

解除標記會同時傳遞至前項和接續工作。 System.Timers.Timer物件用來定義 100 毫秒的逾時值。 如果事件引發,則會 CancellationTokenSource.Cancel 呼叫 方法,並使用取消權杖來要求取消工作。

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);
         }
      }
      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.
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks
Imports Timers = System.Timers

Module Example
   Dim ts As CancellationTokenSource

   Public Sub Main(args() As String)
      Dim upperBound As Integer = If(args.Length >= 1, CInt(args(0)), 200)
      ts = New CancellationTokenSource()
      Dim token As CancellationToken = ts.Token
      Dim timer As New Timers.Timer(100)
      AddHandler timer.Elapsed, AddressOf TimedOutEvent
      timer.AutoReset = False
      timer.Enabled = True

      Dim t1 = Task.Run(Function()
                          ' True = composite.
                          ' False = prime.
                          Dim values(upperBound) As Boolean
                          For ctr = 2 To CInt(Math.Sqrt(upperBound))
                             If values(ctr) = False Then
                                For product = ctr * ctr To upperBound Step ctr
                                   values(product) = True
                                Next
                             End If
                             token.ThrowIfCancellationRequested()
                          Next
                          Return values
                       End Function, token)

      Dim t2 = t1.ContinueWith(Sub(antecedent)
                                  ' Create a list of prime numbers.
                                  Dim primes As New List(Of Integer)()
                                  token.ThrowIfCancellationRequested()
                                  Dim numbers As Boolean() = antecedent.Result
                                  Dim output As String = String.Empty
                                  
                                  For ctr As Integer = 1 To numbers.GetUpperBound(0)
                                     If numbers(ctr) = False Then primes.Add(ctr)
                                  Next

                                  ' Create the output string.
                                  For ctr As Integer = 0 To primes.Count - 1
                                     token.ThrowIfCancellationRequested()
                                     output += primes(ctr).ToString("N0")
                                     If ctr < primes.Count - 1 Then output += ",  "
                                     If (ctr + 1) Mod 8 = 0 Then output += vbCrLf
                                  Next
                                  'Display the result.
                                  Console.WriteLine("Prime numbers from 1 to {0}:{1}",
                                                    upperBound, vbCrLf)
                                  Console.WriteLine(output)
                               End Sub, token)
      Try
         t2.Wait()
      Catch ae As AggregateException
         For Each e In ae.InnerExceptions
            If e.GetType Is GetType(TaskCanceledException) Then
               Console.WriteLine("The operation was cancelled.")
            Else
               Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message)
            End If
         Next
      Finally
         ts.Dispose()
      End Try
   End Sub
   
   Private Sub TimedOutEvent(source As Object, e As Timers.ElapsedEventArgs)
      ts.Cancel()
   End Sub
End Module
' 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.

一般而言,提供大約 100,000 的值會導致逾時間隔到期,並 Timer.Elapsed 引發事件,以及設定取消要求。

備註

傳回 Task 的 將不會排程執行,直到目前的工作完成、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

另請參閱

適用於

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

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

建立當目標 Task<TResult> 完成時,會接收狀態資訊並執行的接續。

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^, System::Object ^> ^ continuationAction, System::Object ^ state);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task<TResult>,object?> continuationAction, object? state);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>, obj> * obj -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult), Object), state As Object) As Task

參數

continuationAction
Action<Task<TResult>,Object>

Task<TResult> 完成時要執行的動作。 執行時,會將已完成的工作和呼叫端提供的狀態物件當做引數傳遞。

state
Object

物件,表示接續動作所使用的資料。

傳回

新的接續 Task

例外狀況

continuationAction 引數為 null

範例

下列範例會建立在 2 到 20 之間傳遞整數的工作,並傳回陣列,其中包含從 n 1 到 n10 到 n10) 之前十個 (指數的陣列。 接著,接續工作會負責顯示指數。 它會同時傳遞前項和原始數位,其指數為前項所產生的數位。

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
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim cts As New CancellationTokenSource()
      Dim token As CancellationToken = cts.Token

      ' Get an integer to generate a list of its exponents.
      Dim rnd As New Random()
      Dim number As Integer = rnd.Next(2, 21)

      Dim t = Task.Factory.StartNew( Function(value)
                                        Dim n As Integer = CInt(value)
                                        Dim values(9) As Long
                                        For ctr As Integer = 1 To 10
                                           values(ctr - 1) = CLng(Math.Pow(n, ctr))
                                        Next
                                        return values
                                     End Function, number)
      Dim continuation = t.ContinueWith( Sub(antecedent, value)
                                            Console.WriteLine("Exponents of {0}:", value)
                                            For ctr As Integer = 0 To 9
                                               Console.WriteLine("   {0} {1} {2} = {3:N0}",
                                                                 value, ChrW(&h02C6), ctr + 1,
                                                                 antecedent.Result(ctr))
                                            Next
                                            Console.WriteLine()
                                         End Sub, number)
      continuation.Wait()

      cts.Dispose()
   End Sub
End Module
' 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

備註

傳回 Task 的 將不會排程執行,直到目前的工作完成、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

另請參閱

適用於

ContinueWith(Action<Task<TResult>>)

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

建立當目標工作完成時非同步執行的接續。

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^> ^ continuationAction);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task<TResult>> continuationAction);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>> -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult))) As Task

參數

continuationAction
Action<Task<TResult>>

當前項 Task<TResult> 完成時要執行的動作。 執行時,會將已完成的工作當做引數傳遞給委派。

傳回

新的接續工作。

例外狀況

continuationAction 引數為 null

範例

下列範例會建立前項工作,其使用 Eratosthenes 的 Sieve 來計算介於 1 與使用者輸入的值之間的質數。 陣列用來保存質數的相關資訊。 陣列索引代表數位,而元素的值會指出該數位是複合的, (其值 true 是) 還是質數 (其值是 false) 。 此工作接著會傳遞至接續工作,負責從整數陣列擷取質數並加以顯示。

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] == false) {
                                      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] == false)
                                                        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
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main(args() As String)
      Dim upperBound As Integer = If(args.Length >= 1, CInt(args(0)), 200)

      Dim t1 = Task.Run(Function()
                          ' True = composite.
                          ' False = prime.
                          Dim values(upperBound) As Boolean
                          For ctr = 2 To CInt(Math.Sqrt(upperBound))
                             If values(ctr) = False Then
                                For product = ctr * ctr To upperBound Step ctr
                                   values(product) = True
                                Next
                             End If
                          Next
                          Return values
                       End Function)

      Dim t2 = t1.ContinueWith(Sub(antecedent)
                                  ' Create a list of prime numbers.
                                  Dim primes As New List(Of Integer)()
                                  Dim numbers As Boolean() = antecedent.Result
                                  Dim output As String = String.Empty
                                  
                                  For ctr As Integer = 1 To numbers.GetUpperBound(0)
                                     If numbers(ctr) = False Then primes.Add(ctr)
                                  Next

                                  ' Create the output string.
                                  For ctr As Integer = 0 To primes.Count - 1
                                     output += primes(ctr).ToString("N0")
                                     If ctr < primes.Count - 1 Then output += ",  "
                                     If (ctr + 1) Mod 8 = 0 Then output += vbCrLf
                                  Next
                                  'Display the result.
                                  Console.WriteLine("Prime numbers from 1 to {0}:{1}",
                                                    upperBound, vbCrLf)
                                  Console.WriteLine(output)
                               End Sub)
      Try
         t2.Wait()
      Catch ae As AggregateException
         For Each e In ae.InnerExceptions
            Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message)
         Next
      End Try
   End Sub
End Module
' 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.

備註

傳回 Task 的 將不會排程執行,直到目前工作完成為止、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

另請參閱

適用於

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

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

建立當目標 Task<TResult> 完成時非同步執行的接續。

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^> ^ continuationAction, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task<TResult>> continuationAction, System.Threading.Tasks.TaskScheduler scheduler);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>> * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult)), scheduler As TaskScheduler) As Task

參數

continuationAction
Action<Task<TResult>>

Task<TResult> 完成時要執行的動作。 執行時,會將已完成的工作當做引數傳遞給委派。

scheduler
TaskScheduler

要與接續工作產生關聯且於執行時使用的 TaskScheduler

傳回

新的接續 Task

例外狀況

continuationAction 引數為 null

-或-

scheduler 引數為 null

備註

傳回 Task 的 將不會排程執行,直到目前的工作完成、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

另請參閱

適用於

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

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

建立目標 Task<TResult> 完成時執行的接續工作。

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, System::Object ^, TNewResult> ^ continuationFunction, System::Object ^ state, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskContinuationOptions continuationOptions, System::Threading::Tasks::TaskScheduler ^ scheduler);
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);
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);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, obj, 'NewResult> * obj * System.Threading.CancellationToken * System.Threading.Tasks.TaskContinuationOptions * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), Object, TNewResult), state As Object, cancellationToken As CancellationToken, continuationOptions As TaskContinuationOptions, scheduler As TaskScheduler) As Task(Of TNewResult)

類型參數

TNewResult

接續作業所產生的結果類型。

參數

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

Task<TResult> 完成時要執行的函式。 當執行時,完成的工作會傳遞至委派,並以呼叫端提供狀態的物件做為引數。

state
Object

物件,表示接續函式所使用的資料。

cancellationToken
CancellationToken

要指派給新工作的 CancellationToken

continuationOptions
TaskContinuationOptions

接續工作的排程時間及其行為方式的適用選項。 這包含準則 (如 OnlyOnCanceled) 和執行選項 (如 ExecuteSynchronously)。

scheduler
TaskScheduler

要與接續工作產生關聯且於執行時使用的 TaskScheduler

傳回

Task<TNewResult>

新的接續 Task<TResult>

例外狀況

scheduler 引數為 null

continuationOptions 引數指定的 TaskContinuationOptions 值不正確。

提供的 CancellationToken 已經過處置。

備註

傳回 Task<TResult> 的 將不會排程執行,直到目前的工作完成、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

執行時,應該 continuationFunction 會傳 Task<TResult> 回 。 此工作的完成狀態將會轉移到從呼叫傳回的工作 Task<TResult>.ContinueWith

另請參閱

適用於

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

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

建立會根據 continuationOptions 中指定的條件執行的接續。

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, TNewResult> ^ continuationFunction, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskContinuationOptions continuationOptions, System::Threading::Tasks::TaskScheduler ^ scheduler);
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);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, 'NewResult> * System.Threading.CancellationToken * System.Threading.Tasks.TaskContinuationOptions * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), TNewResult), cancellationToken As CancellationToken, continuationOptions As TaskContinuationOptions, scheduler As TaskScheduler) As Task(Of TNewResult)

類型參數

TNewResult

接續作業所產生的結果類型。

參數

continuationFunction
Func<Task<TResult>,TNewResult>

要根據 continuationOptions 中指定之條件執行的函式。

執行時,會將這個完成的工作當做引數傳遞給委派。

cancellationToken
CancellationToken

要指派給新工作的 CancellationToken

continuationOptions
TaskContinuationOptions

接續工作的排程時間及其行為方式的適用選項。 這包含準則 (如 OnlyOnCanceled) 和執行選項 (如 ExecuteSynchronously)。

scheduler
TaskScheduler

要與接續工作產生關聯且於執行時使用的 TaskScheduler

傳回

Task<TNewResult>

新的接續 Task<TResult>

例外狀況

Task<TResult> 已經處置。

-或-

已處置建立 cancellationTokenCancellationTokenSource

continuationFunction 引數為 null

-或-

scheduler 引數為 null

continuationOptions 引數指定的 TaskContinuationOptions 值不正確。

備註

傳回 Task<TResult> 的 將不會排程執行,直到目前的工作完成、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

執行時,應該 continuationFunction 會傳 Task<TResult> 回 。

另請參閱

適用於

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

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

建立目標 Task<TResult> 完成時執行的接續工作。

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, System::Object ^, TNewResult> ^ continuationFunction, System::Object ^ state, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult> (Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state, System.Threading.Tasks.TaskScheduler scheduler);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult> (Func<System.Threading.Tasks.Task<TResult>,object?,TNewResult> continuationFunction, object? state, System.Threading.Tasks.TaskScheduler scheduler);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, obj, 'NewResult> * obj * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), Object, TNewResult), state As Object, scheduler As TaskScheduler) As Task(Of TNewResult)

類型參數

TNewResult

接續作業所產生的結果類型。

參數

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

Task<TResult> 完成時要執行的函式。 當執行時,完成的工作會傳遞至委派,並以呼叫端提供狀態的物件做為引數。

state
Object

物件,表示接續函式所使用的資料。

scheduler
TaskScheduler

要與接續工作產生關聯且於執行時使用的 TaskScheduler

傳回

Task<TNewResult>

新的接續 Task<TResult>

例外狀況

scheduler 引數為 null

備註

傳回 Task<TResult> 的 將不會排程執行,直到目前的工作完成、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

另請參閱

適用於

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

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

建立目標 Task<TResult> 完成時執行的接續工作。

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, System::Object ^, TNewResult> ^ continuationFunction, System::Object ^ state, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult> (Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state, System.Threading.CancellationToken cancellationToken);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult> (Func<System.Threading.Tasks.Task<TResult>,object?,TNewResult> continuationFunction, object? state, System.Threading.CancellationToken cancellationToken);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, obj, 'NewResult> * obj * System.Threading.CancellationToken -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), Object, TNewResult), state As Object, cancellationToken As CancellationToken) As Task(Of TNewResult)

類型參數

TNewResult

接續作業所產生的結果類型。

參數

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

Task<TResult> 完成時要執行的函式。 當執行時,完成的工作會傳遞至委派,並以呼叫端提供狀態的物件做為引數。

state
Object

物件,表示接續函式所使用的資料。

cancellationToken
CancellationToken

要指派給新工作的 CancellationToken

傳回

Task<TNewResult>

新的接續 Task<TResult>

例外狀況

continuationFunction 引數為 null

提供的 CancellationToken 已經過處置。

備註

傳回 Task<TResult> 的 將不會排程執行,直到目前的工作完成、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

另請參閱

適用於

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

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

建立目標 Task<TResult> 完成時執行的接續工作。

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, System::Object ^, TNewResult> ^ continuationFunction, System::Object ^ state, System::Threading::Tasks::TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult> (Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult> (Func<System.Threading.Tasks.Task<TResult>,object?,TNewResult> continuationFunction, object? state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, obj, 'NewResult> * obj * System.Threading.Tasks.TaskContinuationOptions -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), Object, TNewResult), state As Object, continuationOptions As TaskContinuationOptions) As Task(Of TNewResult)

類型參數

TNewResult

接續作業所產生的結果類型。

參數

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

Task<TResult> 完成時要執行的函式。 當執行時,完成的工作會傳遞至委派,並以呼叫端提供狀態的物件做為引數。

state
Object

物件,表示接續函式所使用的資料。

continuationOptions
TaskContinuationOptions

接續工作的排程時間及其行為方式的適用選項。 這包含準則 (如 OnlyOnCanceled) 和執行選項 (如 ExecuteSynchronously)。

傳回

Task<TNewResult>

新的接續 Task<TResult>

例外狀況

continuationFunction 引數為 null

continuationOptions 引數指定的 TaskContinuationOptions 值不正確。

備註

傳回 Task<TResult> 的 將不會排程執行,直到目前的工作完成、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

執行時,應該 continuationFunction 會傳 Task<TResult> 回 。 此工作的完成狀態將會轉移到從 ContinueWith 呼叫傳回的工作。

另請參閱

適用於

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

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

建立會根據 continuationOptions 中指定的條件執行的接續。

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, TNewResult> ^ continuationFunction, System::Threading::Tasks::TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult> (Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, 'NewResult> * System.Threading.Tasks.TaskContinuationOptions -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), TNewResult), continuationOptions As TaskContinuationOptions) As Task(Of TNewResult)

類型參數

TNewResult

接續作業所產生的結果類型。

參數

continuationFunction
Func<Task<TResult>,TNewResult>

要根據 continuationOptions 中指定之條件執行的函式。

執行時,會將已完成的工作當做引數傳遞給委派。

continuationOptions
TaskContinuationOptions

接續工作的排程時間及其行為方式的適用選項。 這包含準則 (如 OnlyOnCanceled) 和執行選項 (如 ExecuteSynchronously)。

傳回

Task<TNewResult>

新的接續 Task<TResult>

例外狀況

continuationFunction 引數為 null

continuationOptions 引數指定的 TaskContinuationOptions 值不正確。

備註

傳回 Task<TResult> 的 將不會排程執行,直到目前的工作完成、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

執行時,應該 continuationFunction 會傳 Task<TResult> 回 。

另請參閱

適用於

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

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

建立當目標 Task<TResult> 完成時非同步執行的接續。

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, TNewResult> ^ continuationFunction, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult> (Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction, System.Threading.CancellationToken cancellationToken);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, 'NewResult> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), TNewResult), cancellationToken As CancellationToken) As Task(Of TNewResult)

類型參數

TNewResult

接續作業所產生的結果類型。

參數

continuationFunction
Func<Task<TResult>,TNewResult>

Task<TResult> 完成時要執行的函式。 執行時,會將已完成的工作當做引數傳遞給委派。

cancellationToken
CancellationToken

要指派給新工作的 CancellationToken

傳回

Task<TNewResult>

新的接續 Task<TResult>

例外狀況

Task<TResult> 已經處置。

-或-

已處置建立 cancellationTokenCancellationTokenSource

continuationFunction 引數為 null

備註

傳回 Task<TResult> 的 將不會排程執行,直到目前的工作完成、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

另請參閱

適用於

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

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

建立目標 Task<TResult> 完成時執行的接續工作。

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, System::Object ^, TNewResult> ^ continuationFunction, System::Object ^ state);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult> (Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult> (Func<System.Threading.Tasks.Task<TResult>,object?,TNewResult> continuationFunction, object? state);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, obj, 'NewResult> * obj -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), Object, TNewResult), state As Object) As Task(Of TNewResult)

類型參數

TNewResult

接續作業所產生的結果類型。

參數

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

Task<TResult> 完成時要執行的函式。 當執行時,完成的工作會傳遞至委派,並以呼叫端提供狀態的物件做為引數。

state
Object

物件,表示接續函式所使用的資料。

傳回

Task<TNewResult>

新的接續 Task<TResult>

例外狀況

continuationFunction 引數為 null

範例

下列範例會建立接續工作的鏈結。 每個工作都會提供方法狀態引數的 ContinueWith(Action<Task,Object>, Object) 目前時間 物件 DateTime 。 每個 DateTime 值都代表建立繼續工作的時間。 每個工作會產生第二 DateTime 個值,代表工作完成的時間。 完成所有工作之後,此範例會顯示每個接續工作開始和完成的日期和時間。

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.
*/
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks

' Demonstrates how to associate state with task continuations.
Public Module ContinuationState
   ' Simluates a lengthy operation and returns the time at which
   ' the operation completed.
   Public Function DoWork() As Date
      ' Simulate work by suspending the current thread 
      ' for two seconds.
      Thread.Sleep(2000)

      ' Return the current time.
      Return Date.Now
   End Function

   Public Sub Main()
      ' Start a root task that performs work.
      Dim t As Task(Of Date) = Task(Of Date).Run(Function() DoWork())

      ' Create a chain of continuation tasks, where each task is
      ' followed by another task that performs work.
      Dim continuations As New List(Of Task(Of DateTime))()
      For i As Integer = 0 To 4
         ' Provide the current time as the state of the continuation.
         t = t.ContinueWith(Function(antecedent, state) DoWork(), DateTime.Now)
         continuations.Add(t)
      Next

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

      ' Display the creation time of each continuation (the state object)
      ' and the completion time (the result of that task) to the console.
      For Each continuation In continuations
         Dim start As DateTime = CDate(continuation.AsyncState)
         Dim [end] As DateTime = continuation.Result

         Console.WriteLine("Task was created at {0} and finished at {1}.",
            start.TimeOfDay, [end].TimeOfDay)
      Next
   End Sub
End Module
' The example displays output like the following:
'       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.

備註

傳回 Task<TResult> 的 將不會排程執行,直到目前的工作完成、因為執行成功而完成、因未處理的例外狀況而發生錯誤,或因取消而提早結束。

另請參閱

適用於

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

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

建立當目標 Task<TResult> 完成時非同步執行的接續。

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, TNewResult> ^ continuationFunction);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult> (Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, 'NewResult> -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), TNewResult)) As Task(Of TNewResult)

類型參數

TNewResult

接續作業所產生的結果類型。

參數

continuationFunction
Func<Task<TResult>,TNewResult>

Task<TResult> 完成時要執行的函式。 執行時,會將已完成的工作當做引數傳遞給委派。

傳回

Task<TNewResult>

新的接續 Task<TResult>

例外狀況

continuationFunction 引數為 null

備註

傳回 Task<TResult> 的將不會排程執行,直到目前的工作完成為止,無論是因為順利執行而完成、因未處理的例外狀況而發生錯誤,或因為取消而提早結束。

另請參閱

適用於

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

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

建立當目標 Task<TResult> 完成時非同步執行的接續。

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, TNewResult> ^ continuationFunction, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult> (Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction, System.Threading.Tasks.TaskScheduler scheduler);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, 'NewResult> * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), TNewResult), scheduler As TaskScheduler) As Task(Of TNewResult)

類型參數

TNewResult

接續作業所產生的結果類型。

參數

continuationFunction
Func<Task<TResult>,TNewResult>

Task<TResult> 完成時要執行的函式。 執行時,會將已完成的工作當做引數傳遞給委派。

scheduler
TaskScheduler

要與接續工作產生關聯且於執行時使用的 TaskScheduler

傳回

Task<TNewResult>

新的接續 Task<TResult>

例外狀況

continuationFunction 引數為 null

-或-

scheduler 引數為 null

備註

傳回 Task<TResult> 的將不會排程執行,直到目前的工作完成為止,無論是因為順利執行而完成、因未處理的例外狀況而發生錯誤,或因為取消而提早結束。

另請參閱

適用於