在异步编程中,一个异步操作在完成时调用另一个操作的情况较常见。 延续使后续操作可以使用第一次操作的结果。 传统上,延续性是通过使用回调方法完成的。 在任务并行库 (TPL) 中,延续任务提供了同样的功能。 延续任务(也简称为“延续”)是一个异步任务,在完成时由另一个任务(称为“先行任务”)调用。

尽管延续用起来相对容易,但仍十分强大和灵活。 例如,你可以:

  • 将数据从前面的任务传递到延续。
  • 指定将调用或不调用延续所依据的精确条件。
  • 在延续启动之前取消延续,或在延续正在运行时以协作方式取消延续。
  • 提供有关应如何计划延续的提示。
  • 从同一前面的任务中调用多个延续。
  • 在多个前面的任务中的全部或任意任务完成时调用一个延续。
  • 将延续依次相连,形成任意长度。
  • 使用延续来处理前面的任务所引发的异常。
  • 延续是在 WaitingForActivation 状态下创建的任务。 在一个或多个前面的任务完成时,它将自动激活。 若在用户代码中对延续调用 Task.Start ,将引发 System.InvalidOperationException 异常。

    延续本身是 Task ,并且不阻止它在其上启动的线程。 调用 Task.Wait 方法进行阻止,直到延续任务完成。

    为一个先行任务创建延续

    通过调用 Task.ContinueWith 方法创建在其前面的任务完成时执行的延续。 下面的示例演示基本模式(为清楚起见,省略了异常处理)。 它会执行一个先行任务 taskA ,该任务返回一个 DayOfWeek 对象,指示当天为周几。 当 taskA 完成时, antecedent 会在 ContinueWith 延续方法中表示其结果。 前面任务的结果将被写入控制台。

    using System; using System.Threading.Tasks; public class SimpleExample public static async Task Main() // Declare, assign, and start the antecedent task. Task<DayOfWeek> taskA = Task.Run(() => DateTime.Today.DayOfWeek); // Execute the continuation when the antecedent finishes. await taskA.ContinueWith(antecedent => Console.WriteLine($"Today is {antecedent.Result}.")); // The example displays the following output: // Today is Monday.
    Imports System.Threading.Tasks
    Module Example
        Public Sub Main()
            ' Execute the antecedent.
            Dim taskA As Task(Of DayOfWeek) = Task.Run(Function() DateTime.Today.DayOfWeek)
            ' Execute the continuation when the antecedent finishes.
            Dim continuation As Task = taskA.ContinueWith(Sub(antecedent)
                                                              Console.WriteLine("Today is {0}.", antecedent.Result)
                                                          End Sub)
            continuation.Wait()
        End Sub
    End Module
    ' The example displays output like the following output:
    '       Today is Monday.
    

    为多个先行任务创建延续

    还可以创建一个将在一组任务中的任意或全部任务完成时运行的延续任务。 若要在所有前面的任务都完成后执行延续,则可以调用静态(在 Visual Basic 中为 Shared Task.WhenAll 方法或实例 TaskFactory.ContinueWhenAll 方法。 若要在任意前面的任务完成后执行延续,则可以调用静态(在 Visual Basic 中为 Shared Task.WhenAny 方法或实例 TaskFactory.ContinueWhenAny 方法。

    调用 Task.WhenAll Task.WhenAny 重载不会阻止调用线程。 不过,通常调用除 Task.WhenAll(IEnumerable<Task>) Task.WhenAll(Task[]) 方法外的其他所有方法来检索返回的 Task<TResult>.Result 属性,这样不会阻止调用线程。

    下面的示例调用 Task.WhenAll(IEnumerable<Task>) 方法来创建反映其 10 个前面的任务的结果的延续任务。 每个前面的任务计算从 1 到 10 的索引值的平方值。 如果前面的任务成功完成(其 Task.Status 属性为 TaskStatus.RanToCompletion ),则延续任务的 Task<TResult>.Result 属性为由每个前面的任务返回的 Task<TResult>.Result 值组成的数组。 该示例计算它们的总和,得出 1 到 10 之间的所有数字的平方和:

    using System.Collections.Generic; using System; using System.Threading.Tasks; public class WhenAllExample public static async Task Main() var tasks = new List<Task<int>>(); for (int ctr = 1; ctr <= 10; ctr++) int baseValue = ctr; tasks.Add(Task.Factory.StartNew(b => (int)b! * (int)b, baseValue)); var results = await Task.WhenAll(tasks); int sum = 0; for (int ctr = 0; ctr <= results.Length - 1; ctr++) var result = results[ctr]; Console.Write($"{result} {((ctr == results.Length - 1) ? "=" : "+")} "); sum += result; Console.WriteLine(sum); // The example displays the similar output: // 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 = 385
    Imports System.Collections.Generic
    Imports System.Threading.Tasks
    Module Example
        Public Sub Main()
            Dim tasks As New List(Of Task(Of Integer))()
            For ctr As Integer = 1 To 10
                Dim baseValue As Integer = ctr
                tasks.Add(Task.Factory.StartNew(Function(b)
                                                    Dim i As Integer = CInt(b)
                                                    Return i * i
                                                End Function, baseValue))
            Dim continuation = Task.WhenAll(tasks)
            Dim sum As Long = 0
            For ctr As Integer = 0 To continuation.Result.Length - 1
                Console.Write("{0} {1} ", continuation.Result(ctr),
                              If(ctr = continuation.Result.Length - 1, "=", "+"))
                sum += continuation.Result(ctr)
            Console.WriteLine(sum)
        End Sub
    End Module
    ' The example displays the following output:
    '       1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 = 385
    

    在创建单任务延续时,你可以使用 ContinueWith 重载,该重载采用 System.Threading.Tasks.TaskContinuationOptions 枚举值来指定启动延续所依据的条件。 例如,可以将延续指定为仅在前面的任务已完成运行时运行,或仅在前面的任务完成时处于错误状态时运行。 如果该条件在前面的任务准备调用延续时未得到满足,则延续将直接转换为 TaskStatus.Canceled 状态,之后将无法启动。

    许多多任务延续方法(如 TaskFactory.ContinueWhenAll 方法的重载)也包括 System.Threading.Tasks.TaskContinuationOptions 参数。 但是,只有所有 System.Threading.Tasks.TaskContinuationOptions 枚举成员的一个子集有效。 你可以指定在 System.Threading.Tasks.TaskContinuationOptions 枚举中具有对应的值的 System.Threading.Tasks.TaskCreationOptions 值,如 TaskContinuationOptions.AttachedToParentTaskContinuationOptions.LongRunningTaskContinuationOptions.PreferFairness。 如果为多任务延续指定 NotOnOnlyOn 选项中的任意一个,则在运行时将引发 ArgumentOutOfRangeException 异常。

    有关任务延续选项的详细信息,请参阅 TaskContinuationOptions 一文。

    将数据传递到延续

    Task.ContinueWith 方法将对前面的任务的引用以参数形式传递到延续的用户委托。 如果前面的任务是一个 System.Threading.Tasks.Task<TResult> 对象,并且任务在完成前保持运行,则延续可以访问任务的 Task<TResult>.Result 属性。

    在任务完成之前,将阻止 Task<TResult>.Result 属性。 但是,如果任务已取消或出错,则尝试访问 Result 属性将引发 AggregateException 异常。 可使用 OnlyOnRanToCompletion 选项避免此问题,如下面的示例所示:

    using System; using System.Threading.Tasks; public class ResultExample public static async Task Main() var task = Task.Run( DateTime date = DateTime.Now; return date.Hour > 17 ? "evening" : date.Hour > 12 ? "afternoon" : "morning"; await task.ContinueWith( antecedent => Console.WriteLine($"Good {antecedent.Result}!"); Console.WriteLine($"And how are you this fine {antecedent.Result}?"); }, TaskContinuationOptions.OnlyOnRanToCompletion); // The example displays the similar output: // Good afternoon! // And how are you this fine afternoon?
    Imports System.Threading.Tasks
    Module Example
        Public Sub Main()
            Dim t = Task.Run(Function()
                                 Dim dat As DateTime = DateTime.Now
                                 If dat = DateTime.MinValue Then
                                     Throw New ArgumentException("The clock is not working.")
                                 End If
                                 If dat.Hour > 17 Then
                                     Return "evening"
                                 Else If dat.Hour > 12 Then
                                     Return "afternoon"
                                     Return "morning"
                                 End If
                             End Function)
            Dim c = t.ContinueWith(Sub(antecedent)
                                       Console.WriteLine("Good {0}!",
                                                         antecedent.Result)
                                       Console.WriteLine("And how are you this fine {0}?",
                                                         antecedent.Result)
                                   End Sub, TaskContinuationOptions.OnlyOnRanToCompletion)
            c.Wait()
        End Sub
    End Module
    ' The example displays output like the following:
    '       Good afternoon!
    '       And how are you this fine afternoon?
    

    如果希望延续即使在前面的任务未完成运行时也运行,则必须防止出现异常。 一种方法是测试前面的任务的 Task.Status 属性,并且仅在状态不是 FaultedCanceled 时才尝试访问 Result 属性。 也可以检查前面的任务的 Exception 属性。 有关详细信息,请参阅异常处理。 下面的示例将之前的示例修改为仅在前面的任务的状态为 TaskStatus.RanToCompletion 时,才访问该任务的 Task<TResult>.Result 属性:

    using System; using System.Threading.Tasks; public class ResultTwoExample public static async Task Main() => await Task.Run( DateTime date = DateTime.Now; return date.Hour > 17 ? "evening" : date.Hour > 12 ? "afternoon" : "morning"; .ContinueWith( antecedent => if (antecedent.Status == TaskStatus.RanToCompletion) Console.WriteLine($"Good {antecedent.Result}!"); Console.WriteLine($"And how are you this fine {antecedent.Result}?"); else if (antecedent.Status == TaskStatus.Faulted) Console.WriteLine(antecedent.Exception!.GetBaseException().Message); // The example displays output like the following: // Good afternoon! // And how are you this fine afternoon?
    Imports System.Threading.Tasks
    Module Example
        Public Sub Main()
            Dim t = Task.Run(Function()
                                 Dim dat As DateTime = DateTime.Now
                                 If dat = DateTime.MinValue Then
                                     Throw New ArgumentException("The clock is not working.")
                                 End If
                                 If dat.Hour > 17 Then
                                     Return "evening"
                                 Else If dat.Hour > 12 Then
                                     Return "afternoon"
                                     Return "morning"
                                 End If
                             End Function)
            Dim c = t.ContinueWith(Sub(antecedent)
                                       If t.Status = TaskStatus.RanToCompletion Then
                                           Console.WriteLine("Good {0}!",
                                                             antecedent.Result)
                                           Console.WriteLine("And how are you this fine {0}?",
                                                             antecedent.Result)
                                       Else If t.Status = TaskStatus.Faulted Then
                                           Console.WriteLine(t.Exception.GetBaseException().Message)
                                       End If
                                   End Sub)
        End Sub
    End Module
    ' The example displays output like the following:
    '       Good afternoon!
    '       And how are you this fine afternoon?
    

    在以下情况下,延续的 Task.Status 属性将设置为 TaskStatus.Canceled

  • 延续引发 OperationCanceledException 以响应取消请求。 就像任何任务一样,如果异常包含已传递到延续的相同标记,则会将其视为确认协作取消。

  • System.Threading.CancellationToken 属性为 IsCancellationRequestedtrue传递到延续。 在这种情况下,延续不会启动,并且将转换为 TaskStatus.Canceled 状态。

  • 延续由于其 TaskContinuationOptions 参数设置的条件未得到满足而从不运行。 例如,如果前面的任务进入 TaskStatus.Faulted 状态,则该任务被传递了 TaskContinuationOptions.NotOnFaulted 选项的延续将不会运行,而是将转换为 Canceled 状态。

    如果一项任务及其延续表示同一逻辑操作的两个部分,则可以将相同的取消标记传递到这两个任务,如下面的示例所示。 它包含的前面的任务可生成由可被 33 的整数组成的列表,并将该列表传递给延续。 而延续反过来显示该列表。 前面的任务和延续任务都将定期以随机间隔暂停。 此外, System.Threading.Timer 对象用于在五秒的超时间隔后执行 Elapsed 方法。 此示例调用 CancellationTokenSource.Cancel 方法,从而将导致当前正在执行的任务调用 CancellationToken.ThrowIfCancellationRequested 方法。 是否在前面的任务或其延续正在执行时调用 CancellationTokenSource.Cancel 方法取决于随机生成的暂停的持续时间。 如果已取消前面的任务,则延续将不会启动。 如果未取消前面的任务,则仍然可以使用标记来取消延续。

    using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; public class CancellationExample static readonly Random s_random = new Random((int)DateTime.Now.Ticks); public static async Task Main() using var cts = new CancellationTokenSource(); CancellationToken token = cts.Token; var timer = new Timer(Elapsed, cts, 5000, Timeout.Infinite); var task = Task.Run( async () => var product33 = new List<int>(); for (int index = 1; index < short.MaxValue; index++) if (token.IsCancellationRequested) Console.WriteLine("\nCancellation requested in antecedent...\n"); token.ThrowIfCancellationRequested(); if (index % 2000 == 0) int delay = s_random.Next(16, 501); await Task.Delay(delay); if (index % 33 == 0) product33.Add(index); return product33.ToArray(); }, token); Task<double> continuation = task.ContinueWith( async antecedent => Console.WriteLine("Multiples of 33:\n"); int[] array = antecedent.Result; for (int index = 0; index < array.Length; index++) if (token.IsCancellationRequested) Console.WriteLine("\nCancellation requested in continuation...\n"); token.ThrowIfCancellationRequested(); if (index % 100 == 0) int delay = s_random.Next(16, 251); await Task.Delay(delay); Console.Write($"{array[index]:N0}{(index != array.Length - 1 ? ", " : "")}"); if (Console.CursorLeft >= 74) Console.WriteLine(); Console.WriteLine(); return array.Average(); }, token).Unwrap(); await task; double result = await continuation; catch (Exception ex) Console.WriteLine(ex.Message); Console.WriteLine("\nAntecedent Status: {0}", task.Status); Console.WriteLine("Continuation Status: {0}", continuation.Status); static void Elapsed(object? state) if (state is CancellationTokenSource cts) cts.Cancel(); Console.WriteLine("\nCancellation request issued...\n"); // The example displays the similar output: // Multiples of 33: // 33, 66, 99, 132, 165, 198, 231, 264, 297, 330, 363, 396, 429, 462, 495, 528, // 561, 594, 627, 660, 693, 726, 759, 792, 825, 858, 891, 924, 957, 990, 1,023, // 1,056, 1,089, 1,122, 1,155, 1,188, 1,221, 1,254, 1,287, 1,320, 1,353, 1,386, // 1,419, 1,452, 1,485, 1,518, 1,551, 1,584, 1,617, 1,650, 1,683, 1,716, 1,749, // 1,782, 1,815, 1,848, 1,881, 1,914, 1,947, 1,980, 2,013, 2,046, 2,079, 2,112, // 2,145, 2,178, 2,211, 2,244, 2,277, 2,310, 2,343, 2,376, 2,409, 2,442, 2,475, // 2,508, 2,541, 2,574, 2,607, 2,640, 2,673, 2,706, 2,739, 2,772, 2,805, 2,838, // 2,871, 2,904, 2,937, 2,970, 3,003, 3,036, 3,069, 3,102, 3,135, 3,168, 3,201, // 3,234, 3,267, 3,300, 3,333, 3,366, 3,399, 3,432, 3,465, 3,498, 3,531, 3,564, // 3,597, 3,630, 3,663, 3,696, 3,729, 3,762, 3,795, 3,828, 3,861, 3,894, 3,927, // 3,960, 3,993, 4,026, 4,059, 4,092, 4,125, 4,158, 4,191, 4,224, 4,257, 4,290, // 4,323, 4,356, 4,389, 4,422, 4,455, 4,488, 4,521, 4,554, 4,587, 4,620, 4,653, // 4,686, 4,719, 4,752, 4,785, 4,818, 4,851, 4,884, 4,917, 4,950, 4,983, 5,016, // 5,049, 5,082, 5,115, 5,148, 5,181, 5,214, 5,247, 5,280, 5,313, 5,346, 5,379, // 5,412, 5,445, 5,478, 5,511, 5,544, 5,577, 5,610, 5,643, 5,676, 5,709, 5,742, // Cancellation request issued... // 5,775, // Cancellation requested in continuation... // The operation was canceled. // Antecedent Status: RanToCompletion // Continuation Status: Canceled
    Imports System.Collections.Generic
    Imports System.Threading
    Imports System.Threading.Tasks
    Module Example
        Public Sub Main()
            Dim rnd As New Random()
            Dim lockObj As New Object()
            Dim cts As New CancellationTokenSource()
            Dim token As CancellationToken = cts.Token
            Dim timer As New Timer(AddressOf Elapsed, cts, 5000, Timeout.Infinite)
            Dim t = Task.Run(Function()
                                 Dim product33 As New List(Of Integer)()
                                 For ctr As Integer = 1 To Int16.MaxValue
                                     ' Check for cancellation.
                                     If token.IsCancellationRequested Then
                                         Console.WriteLine("\nCancellation requested in antecedent...\n")
                                         token.ThrowIfCancellationRequested()
                                     End If
                                     ' Introduce a delay.
                                     If ctr Mod 2000 = 0 Then
                                         Dim delay As Integer
                                         SyncLock lockObj
                                             delay = rnd.Next(16, 501)
                                         End SyncLock
                                         Thread.Sleep(delay)
                                     End If
                                     ' Determine if this is a multiple of 33.
                                     If ctr Mod 33 = 0 Then product33.Add(ctr)
                                 Return product33.ToArray()
                             End Function, token)
            Dim continuation = t.ContinueWith(Sub(antecedent)
                                                  Console.WriteLine("Multiples of 33:" + vbCrLf)
                                                  Dim arr = antecedent.Result
                                                  For ctr As Integer = 0 To arr.Length - 1
                                                      If token.IsCancellationRequested Then
                                                          Console.WriteLine("{0}Cancellation requested in continuation...{0}",
                                                                            vbCrLf)
                                                          token.ThrowIfCancellationRequested()
                                                      End If
                                                      If ctr Mod 100 = 0 Then
                                                          Dim delay As Integer
                                                          SyncLock lockObj
                                                              delay = rnd.Next(16, 251)
                                                          End SyncLock
                                                          Thread.Sleep(delay)
                                                      End If
                                                      Console.Write("{0:N0}{1}", arr(ctr),
                                                                    If(ctr <> arr.Length - 1, ", ", ""))
                                                      If Console.CursorLeft >= 74 Then Console.WriteLine()
                                                  Console.WriteLine()
                                              End Sub, token)
                continuation.Wait()
            Catch e As AggregateException
                For Each ie In e.InnerExceptions
                    Console.WriteLine("{0}: {1}", ie.GetType().Name,
                                      ie.Message)
            Finally
                cts.Dispose()
            End Try
            Console.WriteLine(vbCrLf + "Antecedent Status: {0}", t.Status)
            Console.WriteLine("Continuation Status: {0}", continuation.Status)
        End Sub
        Private Sub Elapsed(state As Object)
            Dim cts As CancellationTokenSource = TryCast(state, CancellationTokenSource)
            If cts Is Nothing Then return
            cts.Cancel()
            Console.WriteLine("{0}Cancellation request issued...{0}", vbCrLf)
        End Sub
    End Module
    ' The example displays output like the following:
    '    Multiples of 33:
    '    33, 66, 99, 132, 165, 198, 231, 264, 297, 330, 363, 396, 429, 462, 495, 528,
    '    561, 594, 627, 660, 693, 726, 759, 792, 825, 858, 891, 924, 957, 990, 1,023,
    '    1,056, 1,089, 1,122, 1,155, 1,188, 1,221, 1,254, 1,287, 1,320, 1,353, 1,386,
    '    1,419, 1,452, 1,485, 1,518, 1,551, 1,584, 1,617, 1,650, 1,683, 1,716, 1,749,
    '    1,782, 1,815, 1,848, 1,881, 1,914, 1,947, 1,980, 2,013, 2,046, 2,079, 2,112,
    '    2,145, 2,178, 2,211, 2,244, 2,277, 2,310, 2,343, 2,376, 2,409, 2,442, 2,475,
    '    2,508, 2,541, 2,574, 2,607, 2,640, 2,673, 2,706, 2,739, 2,772, 2,805, 2,838,
    '    2,871, 2,904, 2,937, 2,970, 3,003, 3,036, 3,069, 3,102, 3,135, 3,168, 3,201,
    '    3,234, 3,267, 3,300, 3,333, 3,366, 3,399, 3,432, 3,465, 3,498, 3,531, 3,564,
    '    3,597, 3,630, 3,663, 3,696, 3,729, 3,762, 3,795, 3,828, 3,861, 3,894, 3,927,
    '    3,960, 3,993, 4,026, 4,059, 4,092, 4,125, 4,158, 4,191, 4,224, 4,257, 4,290,
    '    4,323, 4,356, 4,389, 4,422, 4,455, 4,488, 4,521, 4,554, 4,587, 4,620, 4,653,
    '    4,686, 4,719, 4,752, 4,785, 4,818, 4,851, 4,884, 4,917, 4,950, 4,983, 5,016,
    '    5,049, 5,082, 5,115, 5,148, 5,181, 5,214, 5,247, 5,280, 5,313, 5,346, 5,379,
    '    5,412, 5,445, 5,478, 5,511, 5,544, 5,577, 5,610, 5,643, 5,676, 5,709, 5,742,
    '    5,775, 5,808, 5,841, 5,874, 5,907, 5,940, 5,973, 6,006, 6,039, 6,072, 6,105,
    '    6,138, 6,171, 6,204, 6,237, 6,270, 6,303, 6,336, 6,369, 6,402, 6,435, 6,468,
    '    6,501, 6,534, 6,567, 6,600, 6,633, 6,666, 6,699, 6,732, 6,765, 6,798, 6,831,
    '    6,864, 6,897, 6,930, 6,963, 6,996, 7,029, 7,062, 7,095, 7,128, 7,161, 7,194,
    '    7,227, 7,260, 7,293, 7,326, 7,359, 7,392, 7,425, 7,458, 7,491, 7,524, 7,557,
    '    7,590, 7,623, 7,656, 7,689, 7,722, 7,755, 7,788, 7,821, 7,854, 7,887, 7,920,
    '    7,953, 7,986, 8,019, 8,052, 8,085, 8,118, 8,151, 8,184, 8,217, 8,250, 8,283,
    '    8,316, 8,349, 8,382, 8,415, 8,448, 8,481, 8,514, 8,547, 8,580, 8,613, 8,646,
    '    8,679, 8,712, 8,745, 8,778, 8,811, 8,844, 8,877, 8,910, 8,943, 8,976, 9,009,
    '    9,042, 9,075, 9,108, 9,141, 9,174, 9,207, 9,240, 9,273, 9,306, 9,339, 9,372,
    '    9,405, 9,438, 9,471, 9,504, 9,537, 9,570, 9,603, 9,636, 9,669, 9,702, 9,735,
    '    9,768, 9,801, 9,834, 9,867, 9,900, 9,933, 9,966, 9,999, 10,032, 10,065, 10,098,
    '    10,131, 10,164, 10,197, 10,230, 10,263, 10,296, 10,329, 10,362, 10,395, 10,428,
    '    10,461, 10,494, 10,527, 10,560, 10,593, 10,626, 10,659, 10,692, 10,725, 10,758,
    '    10,791, 10,824, 10,857, 10,890, 10,923, 10,956, 10,989, 11,022, 11,055, 11,088,
    '    11,121, 11,154, 11,187, 11,220, 11,253, 11,286, 11,319, 11,352, 11,385, 11,418,
    '    11,451, 11,484, 11,517, 11,550, 11,583, 11,616, 11,649, 11,682, 11,715, 11,748,
    '    11,781, 11,814, 11,847, 11,880, 11,913, 11,946, 11,979, 12,012, 12,045, 12,078,
    '    12,111, 12,144, 12,177, 12,210, 12,243, 12,276, 12,309, 12,342, 12,375, 12,408,
    '    12,441, 12,474, 12,507, 12,540, 12,573, 12,606, 12,639, 12,672, 12,705, 12,738,
    '    12,771, 12,804, 12,837, 12,870, 12,903, 12,936, 12,969, 13,002, 13,035, 13,068,
    '    13,101, 13,134, 13,167, 13,200, 13,233, 13,266,
    '    Cancellation requested in continuation...
    '    Cancellation request issued...
    '    TaskCanceledException: A task was canceled.
    '    Antecedent Status: RanToCompletion
    '    Continuation Status: Canceled
    

    在不向延续提供取消标记而取消其前面的任务的情况下,你也可阻止延续的执行。 通过在创建延续时指定 TaskContinuationOptions.NotOnCanceled 选项来提供标记,如以下示例所示:

    using System; using System.Threading; using System.Threading.Tasks; public class CancellationTwoExample public static async Task Main() using var cts = new CancellationTokenSource(); CancellationToken token = cts.Token; cts.Cancel(); var task = Task.FromCanceled(token); Task continuation = task.ContinueWith( antecedent => Console.WriteLine("The continuation is running."), TaskContinuationOptions.NotOnCanceled); await task; catch (Exception ex) Console.WriteLine($"{ex.GetType().Name}: {ex.Message}"); Console.WriteLine(); Console.WriteLine($"Task {task.Id}: {task.Status:G}"); Console.WriteLine($"Task {continuation.Id}: {continuation.Status:G}"); // The example displays the similar output: // TaskCanceledException: A task was canceled. // Task 1: Canceled // Task 2: Canceled
    Imports System.Threading
    Imports System.Threading.Tasks
    Module Example
        Public Sub Main()
            Dim cts As New CancellationTokenSource()
            Dim token As CancellationToken = cts.Token
            cts.Cancel()
            Dim t As Task = Task.FromCanceled(token)
            Dim continuation As Task = t.ContinueWith(Sub(antecedent)
                                                          Console.WriteLine("The continuation is running.")
                                                      End Sub, TaskContinuationOptions.NotOnCanceled)
                t.Wait()
            Catch e As AggregateException
                For Each ie In e.InnerExceptions
                    Console.WriteLine("{0}: {1}", ie.GetType().Name, ie.Message)
                Console.WriteLine()
            Finally
                cts.Dispose()
            End Try
            Console.WriteLine("Task {0}: {1:G}", t.Id, t.Status)
            Console.WriteLine("Task {0}: {1:G}", continuation.Id,
                              continuation.Status)
        End Sub
    End Module
    ' The example displays the following output:
    '       TaskCanceledException: A task was canceled.
    '       Task 1: Canceled
    '       Task 2: Canceled
    

    在延续转换为 Canceled 状态后,它可能会影响后面的延续,具体情况取决于为这些延续指定的 TaskContinuationOptions

    已释放的延续将不会启动。

    延续和子任务

    在前面的任务及其所有附加的子任务完成之前,延续将不会运行。 延续不会等待分离的子任务完成。 以下两个示例阐释了两个子任务,其中一个附加到创建了延续的前面的任务,另一个从创建了延续的前面的任务中分离。 在下面的示例中,延续仅在所有子任务都完成后才会运行,并且多次运行该示例时,每次生成的输出都相同。 该示例通过调用 TaskFactory.StartNew 方法来启动前面的任务,因为在默认情况下,Task.Run 方法将创建一个默认任务创建选项为 TaskCreationOptions.DenyChildAttach 的父任务。

    using System; using System.Threading.Tasks; public class AttachedExample public static async Task Main() await Task.Factory .StartNew( Console.WriteLine($"Running antecedent task {Task.CurrentId}..."); Console.WriteLine("Launching attached child tasks..."); for (int ctr = 1; ctr <= 5; ctr++) int index = ctr; Task.Factory.StartNew(async value => Console.WriteLine($" Attached child task #{value} running"); await Task.Delay(1000); }, index, TaskCreationOptions.AttachedToParent); Console.WriteLine("Finished launching attached child tasks..."); }).ContinueWith( antecedent => Console.WriteLine($"Executing continuation of Task {antecedent.Id}")); // The example displays the similar output: // Running antecedent task 1... // Launching attached child tasks... // Finished launching attached child tasks... // Attached child task #1 running // Attached child task #5 running // Attached child task #3 running // Attached child task #2 running // Attached child task #4 running // Executing continuation of Task 1
    Imports System.Threading
    Imports System.Threading.Tasks
    Public Module Example
        Public Sub Main()
            Dim t = Task.Factory.StartNew(Sub()
                                              Console.WriteLine("Running antecedent task {0}...",
                                                                Task.CurrentId)
                                              Console.WriteLine("Launching attached child tasks...")
                                              For ctr As Integer = 1 To 5
                                                  Dim index As Integer = ctr
                                                  Task.Factory.StartNew(Sub(value)
                                                                            Console.WriteLine("   Attached child task #{0} running",
                                                                                              value)
                                                                            Thread.Sleep(1000)
                                                                        End Sub, index, TaskCreationOptions.AttachedToParent)
                                              Console.WriteLine("Finished launching attached child tasks...")
                                          End Sub)
            Dim continuation = t.ContinueWith(Sub(antecedent)
                                                  Console.WriteLine("Executing continuation of Task {0}",
                                                                    antecedent.Id)
                                              End Sub)
            continuation.Wait()
        End Sub
    End Module
    ' The example displays the following output:
    '       Running antecedent task 1...
    '       Launching attached child tasks...
    '       Finished launching attached child tasks...
    '          Attached child task #5 running
    '          Attached child task #1 running
    '          Attached child task #2 running
    '          Attached child task #3 running
    '          Attached child task #4 running
    '       Executing continuation of Task 1
    

    但是,如果子任务与前面的任务分离,则前面的任务任务一旦终止,延续就将立即开始运行,而无论子任务的状态。 因此,多次运行下面的示例可能生成可变输出,具体取决于任务计划程序处理每个子任务的方式:

    using System; using System.Threading.Tasks; public class DetachedExample public static async Task Main() Task task = Task.Factory.StartNew( Console.WriteLine($"Running antecedent task {Task.CurrentId}..."); Console.WriteLine("Launching attached child tasks..."); for (int ctr = 1; ctr <= 5; ctr++) int index = ctr; Task.Factory.StartNew( async value => Console.WriteLine($" Attached child task #{value} running"); await Task.Delay(1000); }, index); Console.WriteLine("Finished launching detached child tasks..."); }, TaskCreationOptions.DenyChildAttach); Task continuation = task.ContinueWith( antecedent => Console.WriteLine($"Executing continuation of Task {antecedent.Id}")); await continuation; Console.ReadLine(); // The example displays the similar output: // Running antecedent task 1... // Launching attached child tasks... // Finished launching detached child tasks... // Executing continuation of Task 1 // Attached child task #1 running // Attached child task #5 running // Attached child task #2 running // Attached child task #3 running // Attached child task #4 running
    Imports System.Threading
    Imports System.Threading.Tasks
    Public Module Example
        Public Sub Main()
            Dim t = Task.Factory.StartNew(Sub()
                                              Console.WriteLine("Running antecedent task {0}...",
                                                                Task.CurrentId)
                                              Console.WriteLine("Launching attached child tasks...")
                                              For ctr As Integer = 1 To 5
                                                  Dim index As Integer = ctr
                                                  Task.Factory.StartNew(Sub(value)
                                                                            Console.WriteLine("   Attached child task #{0} running",
                                                                                              value)
                                                                            Thread.Sleep(1000)
                                                                        End Sub, index)
                                              Console.WriteLine("Finished launching detached child tasks...")
                                          End Sub, TaskCreationOptions.DenyChildAttach)
            Dim continuation = t.ContinueWith(Sub(antecedent)
                                                  Console.WriteLine("Executing continuation of Task {0}",
                                                                    antecedent.Id)
                                              End Sub)
            continuation.Wait()
        End Sub
    End Module
    ' The example displays output like the following:
    '       Running antecedent task 1...
    '       Launching attached child tasks...
    '       Finished launching detached child tasks...
    '          Attached child task #1 running
    '          Attached child task #2 running
    '          Attached child task #5 running
    '          Attached child task #3 running
    '       Executing continuation of Task 1
    '          Attached child task #4 running
    

    前面的任务的最终状态取决于任何附加的子任务的最终状态。 分离的子任务的状态不影响父级。 有关详细信息,请参阅附加和分离的子任务

    将状态与延续关联

    可以将任意状态与任务延续关联。 ContinueWith 方法提供重载版本,每个重载版本都带有一个表示延续状态的 Object 值。 可以之后通过使用 Task.AsyncState 属性访问此状态对象。 如果未提供值,则此状态对象为 null

    将使用 异步编程模型 (APM) 的现有代码转换为使用 TPL 时,延续状态非常有用。 在 APM 中,可以在 BeginMethod 方法中提供对象状态,稍后可以使用 IAsyncResult.AsyncState 属性访问该状态。 若要在将使用 APM 的代码转换为使用 TPL 时保留此状态,请使用 ContinueWith 方法。

    在 Visual Studio 调试器中处理 Task 对象时,延续状态也非常有用。 例如,在“并行任务” 窗口中,“任务” 列显示每个任务的状态对象的字符串表示形式。 有关“并行任务”窗口的详细信息,请参阅使用并行任务窗口

    下面的示例演示如何使用延续状态。 它将创建一个延续任务链。 每个任务都将为 DateTime 方法的 state 参数提供当前时间,即一个 ContinueWith 对象。 每个 DateTime 对象都表示创建延续任务的时间。 每个任务都将生成第二个 DateTime 对象作为其结果,该对象表示任务的完成时间。 所有任务都完成后,本示例将显示每个延续任务的创建时间和完成时间。

    using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; class ContinuationStateExample static DateTime DoWork() Thread.Sleep(2000); return DateTime.Now; static async Task Main() Task<DateTime> task = Task.Run(() => DoWork()); var continuations = new List<Task<DateTime>>(); for (int i = 0; i < 5; i++) task = task.ContinueWith((antecedent, _) => DoWork(), DateTime.Now); continuations.Add(task); await task; foreach (Task<DateTime> continuation in continuations) DateTime start = (DateTime)continuation.AsyncState!; DateTime end = continuation.Result; Console.WriteLine($"Task was created at {start.TimeOfDay} and finished at {end.TimeOfDay}."); Console.ReadLine(); // The example displays the similar 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
        ' Simulates 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)
            ' 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)
        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 类型的延续

    有时,可能需要链接可返回 Task 类型的延续。 这些任务称为嵌套任务。 当父任务调用 Task<TResult>.ContinueWith,并提供作为任务返回的 continuationFunction 时,你可以调用 Unwrap 来创建表示 <Task<Task<T>>>Task(Of Task(Of T)) (Visual Basic) 的异步操作的代理任务。

    下面的示例展示如何使用包装附加任务返回函数的延续。 每个延续都可以进行解包,并公开已包装的内部任务。

    using System; using System.Threading; using System.Threading.Tasks; public class UnwrapExample public static async Task Main() Task<int> taskOne = RemoteIncrement(0); Console.WriteLine("Started RemoteIncrement(0)"); Task<int> taskTwo = RemoteIncrement(4) .ContinueWith(t => RemoteIncrement(t.Result)) .Unwrap().ContinueWith(t => RemoteIncrement(t.Result)) .Unwrap().ContinueWith(t => RemoteIncrement(t.Result)) .Unwrap(); Console.WriteLine("Started RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...)"); await taskOne; Console.WriteLine("Finished RemoteIncrement(0)"); await taskTwo; Console.WriteLine("Finished RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...)"); catch (Exception e) Console.WriteLine($"A task has thrown the following (unexpected) exception:\n{e}"); static Task<int> RemoteIncrement(int number) => Task<int>.Factory.StartNew( obj => Thread.Sleep(1000); int x = (int)(obj!); Console.WriteLine("Thread={0}, Next={1}", Thread.CurrentThread.ManagedThreadId, ++x); return x; number); // The example displays the similar output: // Started RemoteIncrement(0) // Started RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...) // Thread=4, Next=1 // Finished RemoteIncrement(0) // Thread=5, Next=5 // Thread=6, Next=6 // Thread=6, Next=7 // Thread=6, Next=8 // Finished RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...) Imports System.Threading Module UnwrapExample Sub Main() Dim taskOne As Task(Of Integer) = RemoteIncrement(0) Console.WriteLine("Started RemoteIncrement(0)") Dim taskTwo As Task(Of Integer) = RemoteIncrement(4). ContinueWith(Function(t) RemoteIncrement(t.Result)). Unwrap().ContinueWith(Function(t) RemoteIncrement(t.Result)). Unwrap().ContinueWith(Function(t) RemoteIncrement(t.Result)). Unwrap() Console.WriteLine("Started RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...)") taskOne.Wait() Console.WriteLine("Finished RemoteIncrement(0)") taskTwo.Wait() Console.WriteLine("Finished RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...)") Catch e As AggregateException Console.WriteLine($"A task has thrown the following (unexpected) exception:{vbLf}{e}") End Try End Sub Function RemoteIncrement(ByVal number As Integer) As Task(Of Integer) Return Task(Of Integer).Factory.StartNew( Function(obj) Thread.Sleep(1000) Dim x As Integer = CInt(obj) Console.WriteLine("Thread={0}, Next={1}", Thread.CurrentThread.ManagedThreadId, Interlocked.Increment(x)) Return x End Function, number) End Function End Module ' The example displays the similar output: ' Started RemoteIncrement(0) ' Started RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...) ' Thread=4, Next=1 ' Finished RemoteIncrement(0) ' Thread=5, Next=5 ' Thread=6, Next=6 ' Thread=6, Next=7 ' Thread=6, Next=8 ' Finished RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...)

    有关使用 Unwrap 的详细信息,请参阅如何:展开嵌套任务

    处理从延续中引发的异常

    前面的任务与延续之间的关系不是父/子关系。 由延续引发的异常不会传播到前面的任务。 因此,请按在任何其他任务中处理异常的方式来处理由延续引发的异常,如下所示:

  • 你可以使用 WaitWaitAllWaitAny 法或其对应的泛型方法来等待延续。 你可以在同一 try 语句中等待前面的任务及其延续,如下面的示例所示:
  • using System; using System.Threading.Tasks; public class ExceptionExample public static async Task Main() Task<int> task = Task.Run( Console.WriteLine($"Executing task {Task.CurrentId}"); return 54; var continuation = task.ContinueWith( antecedent => Console.WriteLine($"Executing continuation task {Task.CurrentId}"); Console.WriteLine($"Value from antecedent: {antecedent.Result}"); throw new InvalidOperationException(); await task; await continuation; catch (Exception ex) Console.WriteLine(ex.Message); // The example displays the similar output: // Executing task 1 // Executing continuation task 2 // Value from antecedent: 54 // Operation is not valid due to the current state of the object.
    Imports System.Threading.Tasks
    Module Example
        Public Sub Main()
            Dim task1 = Task(Of Integer).Run(Function()
                                                 Console.WriteLine("Executing task {0}",
                                                                   Task.CurrentId)
                                                 Return 54
                                             End Function)
            Dim continuation = task1.ContinueWith(Sub(antecedent)
                                                      Console.WriteLine("Executing continuation task {0}",
                                                                        Task.CurrentId)
                                                      Console.WriteLine("Value from antecedent: {0}",
                                                                        antecedent.Result)
                                                      Throw New InvalidOperationException()
                                                  End Sub)
                task1.Wait()
                continuation.Wait()
            Catch ae As AggregateException
                For Each ex In ae.InnerExceptions
                    Console.WriteLine(ex.Message)
            End Try
        End Sub
    End Module
    ' The example displays the following output:
    '       Executing task 1
    '       Executing continuation task 2
    '       Value from antecedent: 54
    '       Operation is not valid due to the current state of the object.
    
  • 可以使用第二个延续来观察第一个延续的 Exception 属性。 在下面的示例中,某个任务尝试从不存在的文件中进行读取。 然后,延续将显示有关前面的任务中的异常的信息。
  • using System; using System.IO; using System.Linq; using System.Threading.Tasks; public class ExceptionTwoExample public static async Task Main() var task = Task.Run( string fileText = File.ReadAllText(@"C:\NonexistentFile.txt"); return fileText; Task continuation = task.ContinueWith( antecedent => var fileNotFound = antecedent.Exception ?.InnerExceptions ?.FirstOrDefault(e => e is FileNotFoundException) as FileNotFoundException; if (fileNotFound != null) Console.WriteLine(fileNotFound.Message); }, TaskContinuationOptions.OnlyOnFaulted); await continuation; Console.ReadLine(); // The example displays the following output: // Could not find file 'C:\NonexistentFile.txt'.
    Imports System.IO
    Imports System.Threading.Tasks
    Module Example
        Public Sub Main()
            Dim t = Task.Run(Function()
                                 Dim s As String = File.ReadAllText("C:\NonexistentFile.txt")
                                 Return s
                             End Function)
            Dim c = t.ContinueWith(Sub(antecedent)
                                       ' Get the antecedent's exception information.
                                       For Each ex In antecedent.Exception.InnerExceptions
                                           If TypeOf ex Is FileNotFoundException
                                               Console.WriteLine(ex.Message)
                                           End If
                                   End Sub, TaskContinuationOptions.OnlyOnFaulted)
            c.Wait()
        End Sub
    End Module
    ' The example displays the following output:
    '       Could not find file 'C:\NonexistentFile.txt'.
    

    因为使用 TaskContinuationOptions.OnlyOnFaulted 选项运行,所以仅当前面的任务中发生异常时才会执行延续。 因此,它可以假设前面的任务的 Exception 的属性不为 null。 如果无论前面的任务中是否引发了异常都将执行延续,则必须先检查前面的任务的 Exception 属性是否不为 null,然后再尝试处理该异常,如以下代码片段所示:

    var fileNotFound = antecedent.Exception ?.InnerExceptions ?.FirstOrDefault(e => e is FileNotFoundException) as FileNotFoundException; if (fileNotFound != null) Console.WriteLine(fileNotFound.Message);
    ' Determine whether an exception occurred.
    If antecedent.Exception IsNot Nothing Then
        ' Get the antecedent's exception information.
        For Each ex In antecedent.Exception.InnerExceptions
            If TypeOf ex Is FileNotFoundException
                Console.WriteLine(ex.Message)
            End If
    End If
    

    有关详细信息,请参阅异常处理

  • 如果延续为附加子任务并且是通过使用 TaskContinuationOptions.AttachedToParent 选项创建的,则父级会将该延续的异常传播回调用线程,就像任何其他附加子级的情况一样。 有关详细信息,请参阅附加和分离的子任务
  • 任务并行库 (TPL)
  •