Task.ContinueWith 方法

定義

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

多載

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

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊和取消語彙基元,並執行的接續。 接續會根據一組指定的條件執行,並使用指定的排程器。

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

建立當目標工作完成時,會根據指定之 TaskContinuationOptions 執行的接續。 接續會接收取消語彙基元,並使用指定的排程器。

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

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊並非同步執行的接續。 接續會使用指定的排程器。

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

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊並執行的接續。 接續會根據一組指定的條件執行。

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

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊和取消語彙基元,並非同步執行的接續。

ContinueWith(Action<Task>)

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

ContinueWith(Action<Task>, TaskContinuationOptions)

建立當目標工作完成時,會根據指定之 TaskContinuationOptions 執行的接續。

ContinueWith(Action<Task>, CancellationToken)

建立當目標 Task 完成時,會接收取消語彙基元並非同步執行的接續。

ContinueWith(Action<Task,Object>, Object)

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊並執行的接續。

ContinueWith(Action<Task>, TaskScheduler)

建立當目標 Task 完成時非同步執行的接續。 接續會使用指定的排程器。

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

建立當目標 Task 完成時非同步執行的接續。 接續會接收呼叫端提供的狀態資訊,並使用指定的排程器。

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

建立當目標 Task 完成並傳回值時,會根據指定的工作接續選項來執行的接續。 接續會接收呼叫端提供的狀態資訊和取消語彙基元,並使用指定的排程器。

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

建立會根據指定之接續選項執行並傳回值的接續。 接續會接收取消語彙基元,並使用指定的排程器。

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

建立當目標 Task 完成並傳回值時非同步執行的接續。 接續會接收呼叫端提供的狀態資訊和取消語彙基元。

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

建立當目標 Task 完成時,會根據指定的工作接續選項來執行的接續。 接續會接收呼叫端提供的狀態資訊。

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

建立會根據指定之接續選項執行並傳回值的接續。

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

建立當目標 Task 完成並傳回值時非同步執行的接續。 接續會使用指定的排程器。

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

建立當目標 Task 完成並傳回值時非同步執行的接續。 接續會接收取消語彙基元。

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

建立當目標 Task 完成並傳回值時,會接收呼叫端提供的狀態資訊並非同步執行的接續。

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

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

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

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊和取消語彙基元,並執行的接續。 接續會根據一組指定的條件執行,並使用指定的排程器。

C#
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task,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,object?> continuationAction, object? state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);

參數

continuationAction
Action<Task,Object>

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

state
Object

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

cancellationToken
CancellationToken

要指派給新接續工作的 CancellationToken

continuationOptions
TaskContinuationOptions

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

scheduler
TaskScheduler

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

傳回

新的接續 Task

例外狀況

scheduler 引數為 null

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

提供的 CancellationToken 已經過處置。

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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>, CancellationToken, TaskContinuationOptions, TaskScheduler)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標工作完成時,會根據指定之 TaskContinuationOptions 執行的接續。 接續會接收取消語彙基元,並使用指定的排程器。

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

參數

continuationAction
Action<Task>

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

cancellationToken
CancellationToken

要指派給新接續工作的 CancellationToken

continuationOptions
TaskContinuationOptions

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

scheduler
TaskScheduler

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

傳回

新的接續 Task

例外狀況

已處置建立語彙基元的 CancellationTokenSource

continuationAction 引數為 null。

-或-

scheduler 引數為 null。

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

範例

以下是使用 ContinueWith 在背景和使用者介面線程上執行工作的範例。

C#
private void Button1_Click(object sender, EventArgs e)
{
   var backgroundScheduler = TaskScheduler.Default;
   var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
   Task.Factory.StartNew(delegate { DoBackgroundComputation(); },
                         backgroundScheduler).
   ContinueWith(delegate { UpdateUI(); }, uiScheduler).
                ContinueWith(delegate { DoAnotherBackgroundComputation(); },
                             backgroundScheduler).
                ContinueWith(delegate { UpdateUIAgain(); }, uiScheduler);
}

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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,Object>, Object, TaskScheduler)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊並非同步執行的接續。 接續會使用指定的排程器。

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

參數

continuationAction
Action<Task,Object>

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

state
Object

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

scheduler
TaskScheduler

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

傳回

新的接續 Task

例外狀況

scheduler 引數為 null

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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,Object>, Object, TaskContinuationOptions)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊並執行的接續。 接續會根據一組指定的條件執行。

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

參數

continuationAction
Action<Task,Object>

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

state
Object

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

continuationOptions
TaskContinuationOptions

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

傳回

新的接續 Task

例外狀況

continuationAction 引數為 null

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

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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,Object>, Object, CancellationToken)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊和取消語彙基元,並非同步執行的接續。

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

參數

continuationAction
Action<Task,Object>

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

state
Object

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

cancellationToken
CancellationToken

要指派給新接續工作的 CancellationToken

傳回

新的接續 Task

例外狀況

continuationAction 引數為 null

提供的 CancellationToken 已經過處置。

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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>)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

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

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

參數

continuationAction
Action<Task>

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

傳回

新的接續 Task

例外狀況

continuationAction 引數為 null

範例

下列範例會定義以100個隨機日期和時間值填入陣列的工作。 它會使用 ContinueWith(Action<Task>) 方法,在陣列完全填入之後選取最早和最新的日期值。

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

public class Example
{
   public static void Main()
   {
      var firstTask = Task.Factory.StartNew( () => {
                               Random rnd = new Random();
                               DateTime[] dates = new DateTime[100];
                               Byte[] buffer = new Byte[8];
                               int ctr = dates.GetLowerBound(0);
                               while (ctr <= dates.GetUpperBound(0)) {
                                  rnd.NextBytes(buffer);
                                  long ticks = BitConverter.ToInt64(buffer, 0);
                                  if (ticks <= DateTime.MinValue.Ticks | ticks >= DateTime.MaxValue.Ticks)
                                     continue;

                                  dates[ctr] = new DateTime(ticks);
                                  ctr++;
                               }
                               return dates;
                            } ); 
                         
      Task continuationTask = firstTask.ContinueWith( (antecedent) => {
                             DateTime[] dates = antecedent.Result;
                             DateTime earliest = dates[0];
                             DateTime latest = earliest;
                             
                             for (int ctr = dates.GetLowerBound(0) + 1; ctr <= dates.GetUpperBound(0); ctr++) {
                                if (dates[ctr] < earliest) earliest = dates[ctr];
                                if (dates[ctr] > latest) latest = dates[ctr];
                             }
                             Console.WriteLine("Earliest date: {0}", earliest);
                             Console.WriteLine("Latest date: {0}", latest);
                          } );                      
      // Since a console application otherwise terminates, wait for the continuation to complete.
     continuationTask.Wait();
   }
}
// The example displays output like the following:
//       Earliest date: 2/11/0110 12:03:41 PM
//       Latest date: 7/29/9989 2:14:49 PM

由於主控台應用程式可能會在接續工作執行之前終止, Wait() 因此會呼叫 方法,以確保接續會在範例結束之前完成執行。

如需其他範例,請參閱 使用接續工作鏈結工作

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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>, TaskContinuationOptions)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標工作完成時,會根據指定之 TaskContinuationOptions 執行的接續。

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

參數

continuationAction
Action<Task>

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

continuationOptions
TaskContinuationOptions

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

傳回

新的接續 Task

例外狀況

continuationAction 引數為 null。

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

範例

下列範例示範如何使用 TaskContinuationOptions 指定當前項工作完成時,接續工作應該同步執行。 (如果呼叫指定的工作已完成 ContinueWith ,同步接續會在呼叫 ContinueWith.) 的線程上執行

C#
public class TaskCounter
{
   private volatile int _count;

   public void Track(Task t)
   {
      if (t == null) throw new ArgumentNullException("t");
      Interlocked.Increment(ref _count);
      t.ContinueWith(ct => Interlocked.Decrement(ref _count), TaskContinuationOptions.ExecuteSynchronously);
   }

   public int NumberOfActiveTasks { get { return _count; } }
}

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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>, CancellationToken)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成時,會接收取消語彙基元並非同步執行的接續。

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

參數

continuationAction
Action<Task>

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

cancellationToken
CancellationToken

要指派給新接續工作的 CancellationToken

傳回

新的接續 Task

例外狀況

已處置建立語彙基元的 CancellationTokenSource

continuationAction 引數為 null。

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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,Object>, Object)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊並執行的接續。

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

參數

continuationAction
Action<Task,Object>

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

state
Object

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

傳回

新的接續工作。

例外狀況

continuationAction 引數為 null

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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>, TaskScheduler)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成時非同步執行的接續。 接續會使用指定的排程器。

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

參數

continuationAction
Action<Task>

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

scheduler
TaskScheduler

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

傳回

新的接續 Task

例外狀況

Task 已經處置。

continuationAction 引數為 null

-或-

scheduler 引數為 null。

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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<TResult>(Func<Task,Object,TResult>, Object, TaskScheduler)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成時非同步執行的接續。 接續會接收呼叫端提供的狀態資訊,並使用指定的排程器。

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

類型參數

TResult

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

參數

continuationFunction
Func<Task,Object,TResult>

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

state
Object

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

scheduler
TaskScheduler

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

傳回

新的接續 Task<TResult>

例外狀況

scheduler 引數為 null

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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<TResult>(Func<Task,Object,TResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成並傳回值時,會根據指定的工作接續選項來執行的接續。 接續會接收呼叫端提供的狀態資訊和取消語彙基元,並使用指定的排程器。

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

類型參數

TResult

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

參數

continuationFunction
Func<Task,Object,TResult>

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

state
Object

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

cancellationToken
CancellationToken

要指派給新接續工作的 CancellationToken

continuationOptions
TaskContinuationOptions

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

scheduler
TaskScheduler

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

傳回

新的接續 Task<TResult>

例外狀況

scheduler 引數為 null

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

提供的 CancellationToken 已經過處置。

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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<TResult>(Func<Task,TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立會根據指定之接續選項執行並傳回值的接續。 接續會接收取消語彙基元,並使用指定的排程器。

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

類型參數

TResult

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

參數

continuationFunction
Func<Task,TResult>

根據指定的 continuationOptions. [執行時] 執行的函式,委派將會以自變數的形式傳遞已完成的工作。

cancellationToken
CancellationToken

要指派給新接續工作的 CancellationToken

continuationOptions
TaskContinuationOptions

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

scheduler
TaskScheduler

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

傳回

新的接續 Task<TResult>

例外狀況

Task 已經處置。

-或-

已處置建立語彙基元的 CancellationTokenSource

continuationFunction 引數為 null。

-或-

scheduler 引數為 null。

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

範例

下列範例示範如何搭配接續選項使用 ContinueWith 方法:

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

class ContinuationOptionsDemo
{
    // Demonstrated features:
    //      TaskContinuationOptions
    //		Task.ContinueWith()
    // 		Task.Factory
    //		Task.Wait()
    // Expected results:
    // 		This sample demonstrates branched continuation sequences - Task+Commit or Task+Rollback.
    //      Notice that no if statements are used.
    //		The first sequence is successful - tran1 and commitTran1 are executed. rollbackTran1 is canceled.
    //		The second sequence is unsuccessful - tran2 and rollbackTran2 are executed. tran2 is faulted, and commitTran2 is canceled.
    // Documentation:
    //		http://msdn.microsoft.com/library/system.threading.tasks.taskcontinuationoptions(VS.100).aspx
    static void Main()
    {
        Action success = () => Console.WriteLine("Task={0}, Thread={1}: Begin successful transaction",
                                                Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
        Action failure = () =>
        {
            Console.WriteLine("Task={0}, Thread={1}: Begin transaction and encounter an error",
                                Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
            throw new InvalidOperationException("SIMULATED EXCEPTION");
        };

        Action<Task> commit = (antecendent) => Console.WriteLine("Task={0}, Thread={1}: Commit transaction",
                                                                Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
        Action<Task> rollback = (antecendent) =>
        {
            // "Observe" your antecedent's exception so as to avoid an exception
            // being thrown on the finalizer thread
            var unused = antecendent.Exception;

            Console.WriteLine("Task={0}, Thread={1}: Rollback transaction",
                  Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
        };

        // Successful transaction - Begin + Commit
        Console.WriteLine("Demonstrating a successful transaction");

        // Initial task
        // Treated as "fire-and-forget" -- any exceptions will be cleaned up in rollback continuation
        Task tran1 = Task.Factory.StartNew(success);

        // The following task gets scheduled only if tran1 completes successfully
        var commitTran1 = tran1.ContinueWith(commit, TaskContinuationOptions.OnlyOnRanToCompletion);

        // The following task gets scheduled only if tran1 DOES NOT complete successfully
        var rollbackTran1 = tran1.ContinueWith(rollback, TaskContinuationOptions.NotOnRanToCompletion);

        // For demo purposes, wait for the sample to complete
        commitTran1.Wait();

        // -----------------------------------------------------------------------------------

        // Failed transaction - Begin + exception + Rollback
        Console.WriteLine("\nDemonstrating a failed transaction");

        // Initial task
        // Treated as "fire-and-forget" -- any exceptions will be cleaned up in rollback continuation
        Task tran2 = Task.Factory.StartNew(failure);

        // The following task gets scheduled only if tran2 completes successfully
        var commitTran2 = tran2.ContinueWith(commit, TaskContinuationOptions.OnlyOnRanToCompletion);

        // The following task gets scheduled only if tran2 DOES NOT complete successfully
        var rollbackTran2 = tran2.ContinueWith(rollback, TaskContinuationOptions.NotOnRanToCompletion);

        // For demo purposes, wait for the sample to complete
        rollbackTran2.Wait();
    }
}

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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<TResult>(Func<Task,Object,TResult>, Object, CancellationToken)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成並傳回值時非同步執行的接續。 接續會接收呼叫端提供的狀態資訊和取消語彙基元。

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

類型參數

TResult

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

參數

continuationFunction
Func<Task,Object,TResult>

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

state
Object

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

cancellationToken
CancellationToken

要指派給新接續工作的 CancellationToken

傳回

新的接續 Task<TResult>

例外狀況

continuationFunction 引數為 null

提供的 CancellationToken 已經過處置。

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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<TResult>(Func<Task,Object,TResult>, Object, TaskContinuationOptions)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成時,會根據指定的工作接續選項來執行的接續。 接續會接收呼叫端提供的狀態資訊。

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

類型參數

TResult

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

參數

continuationFunction
Func<Task,Object,TResult>

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

state
Object

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

continuationOptions
TaskContinuationOptions

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

傳回

新的接續 Task<TResult>

例外狀況

continuationFunction 引數為 null

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

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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<TResult>(Func<Task,TResult>, TaskContinuationOptions)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立會根據指定之接續選項執行並傳回值的接續。

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

類型參數

TResult

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

參數

continuationFunction
Func<Task,TResult>

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

continuationOptions
TaskContinuationOptions

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

傳回

新的接續 Task<TResult>

例外狀況

Task 已經處置。

continuationFunction 引數為 null。

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

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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<TResult>(Func<Task,TResult>, TaskScheduler)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成並傳回值時非同步執行的接續。 接續會使用指定的排程器。

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

類型參數

TResult

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

參數

continuationFunction
Func<Task,TResult>

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

scheduler
TaskScheduler

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

傳回

新的接續 Task<TResult>

例外狀況

Task 已經處置。

continuationFunction 引數為 null。

-或-

scheduler 引數為 null。

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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<TResult>(Func<Task,TResult>, CancellationToken)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成並傳回值時非同步執行的接續。 接續會接收取消語彙基元。

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

類型參數

TResult

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

參數

continuationFunction
Func<Task,TResult>

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

cancellationToken
CancellationToken

要指派給新接續工作的 CancellationToken

傳回

新的接續 Task<TResult>

例外狀況

Task 已經處置。

-或-

已處置建立語彙基元的 CancellationTokenSource

continuationFunction 引數為 null。

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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<TResult>(Func<Task,Object,TResult>, Object)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

建立當目標 Task 完成並傳回值時,會接收呼叫端提供的狀態資訊並非同步執行的接續。

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

類型參數

TResult

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

參數

continuationFunction
Func<Task,Object,TResult>

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

state
Object

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

傳回

新的接續 Task<TResult>

例外狀況

continuationFunction 引數為 null

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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<TResult>(Func<Task,TResult>)

來源:
Task.cs
來源:
Task.cs
來源:
Task.cs

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

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

類型參數

TResult

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

參數

continuationFunction
Func<Task,TResult>

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

傳回

新的接續工作。

例外狀況

Task 已經處置。

continuationFunction 引數為 null。

範例

下列範例示範如何使用 ContinueWith 方法:

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

class ContinuationSimpleDemo
{
    // Demonstrated features:
    // 		Task.Factory
    //		Task.ContinueWith()
    //		Task.Wait()
    // Expected results:
    // 		A sequence of three unrelated tasks is created and executed in this order - alpha, beta, gamma.
    //		A sequence of three related tasks is created - each task negates its argument and passes is to the next task: 5, -5, 5 is printed.
    //		A sequence of three unrelated tasks is created where tasks have different types.
    // Documentation:
    //		http://msdn.microsoft.com/library/system.threading.tasks.taskfactory_members(VS.100).aspx
    static void Main()
    {
        Action<string> action =
            (str) =>
                Console.WriteLine("Task={0}, str={1}, Thread={2}", Task.CurrentId, str, Thread.CurrentThread.ManagedThreadId);

        // Creating a sequence of action tasks (that return no result).
        Console.WriteLine("Creating a sequence of action tasks (that return no result)");
        Task.Factory.StartNew(() => action("alpha"))
            .ContinueWith(antecendent => action("beta"))        // Antecedent data is ignored
            .ContinueWith(antecendent => action("gamma"))
            .Wait();

        Func<int, int> negate =
            (n) =>
            {
                Console.WriteLine("Task={0}, n={1}, -n={2}, Thread={3}", Task.CurrentId, n, -n, Thread.CurrentThread.ManagedThreadId);
                return -n;
            };

        // Creating a sequence of function tasks where each continuation uses the result from its antecendent
        Console.WriteLine("\nCreating a sequence of function tasks where each continuation uses the result from its antecendent");
        Task<int>.Factory.StartNew(() => negate(5))
            .ContinueWith(antecendent => negate(antecendent.Result))		// Antecedent result feeds into continuation
            .ContinueWith(antecendent => negate(antecendent.Result))
            .Wait();

        // Creating a sequence of tasks where you can mix and match the types
        Console.WriteLine("\nCreating a sequence of tasks where you can mix and match the types");
        Task<int>.Factory.StartNew(() => negate(6))
            .ContinueWith(antecendent => action("x"))
            .ContinueWith(antecendent => negate(7))
            .Wait();
    }
}

備註

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

適用於

.NET 9 及其他版本
產品 版本
.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
.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