当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET Task.ContinueWith方法代码示例

本文整理汇总了VB.NET中System.Threading.Tasks.Task.ContinueWith方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Task.ContinueWith方法的具体用法?VB.NET Task.ContinueWith怎么用?VB.NET Task.ContinueWith使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Threading.Tasks.Task的用法示例。


在下文中一共展示了Task.ContinueWith方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: Example

' 导入命名空间
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim firstTask = Task.Factory.StartNew( Function()
                               Dim rnd As New Random()
                               Dim dates(99) As Date
                               Dim buffer(7) As Byte
                               Dim ctr As Integer = dates.GetLowerBound(0)
                               Do While ctr <= dates.GetUpperBound(0)
                                  rnd.NextBytes(buffer)
                                  Dim ticks As Long = BitConverter.ToInt64(buffer, 0)
                                  If ticks <= DateTime.MinValue.Ticks Or ticks >= DateTime.MaxValue.Ticks Then Continue Do

                                  dates(ctr) = New Date(ticks)
                                  ctr += 1
                               Loop
                               Return dates
                            End Function )
                         
      Dim continuationTask As Task = firstTask.ContinueWith( Sub(antecedent)
                             Dim dates() As Date = antecedent.Result
                             Dim earliest As Date = dates(0)
                             Dim latest As Date = earliest
                             
                             For ctr As Integer = dates.GetLowerBound(0) + 1 To dates.GetUpperBound(0)
                                If dates(ctr) < earliest Then earliest = dates(ctr)
                                If dates(ctr) > latest Then latest = dates(ctr)
                             Next
                             Console.WriteLine("Earliest date: {0}", earliest)
                             Console.WriteLine("Latest date: {0}", latest)
                          End Sub)                      
      ' Since a console application otherwise terminates, wait for the continuation to complete.
      continuationTask.Wait()
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:37,代码来源:Task.ContinueWith

输出:

Earliest date: 2/11/0110 12:03:41 PM
Latest date: 7/29/9989 2:14:49 PM

示例2: ContuationOptionsDemo

' 导入命名空间
Imports System.Threading
Imports System.Threading.Tasks

Module ContuationOptionsDemo
    ' 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
    Private Sub Main()
        Dim success As Action = Sub()
                                    Console.WriteLine("Task={0}, Thread={1}: Begin successful transaction", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
                                End Sub

        Dim failure As Action = Sub()
                                    Console.WriteLine("Task={0}, Thread={1}: Begin transaction and encounter an error", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
                                    Throw New InvalidOperationException("SIMULATED EXCEPTION")
                                End Sub

        Dim commit As Action(Of Task) = Sub(antecendent)
                                            Console.WriteLine("Task={0}, Thread={1}: Commit transaction", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
                                        End Sub

        Dim rollback As Action(Of Task) = Sub(antecendent)
                                              ' "Observe" your antecedent's exception so as to avoid an exception
                                              ' being thrown on the finalizer thread
                                              Dim unused = antecendent.Exception

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

        ' 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
        Dim tran1 As Task = Task.Factory.StartNew(success)

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

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

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

        ' -----------------------------------------------------------------------------------


        ' Failed transaction - Begin + exception + Rollback 
        Console.WriteLine(vbLf & "Demonstrating a failed transaction")

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

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

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

        ' For demo purposes, wait for the sample to complete
        rollbackTran2.Wait()
    End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:75,代码来源:Task.ContinueWith

示例3: ContinuationDemo

' 导入命名空间
Imports System.Threading
Imports System.Threading.Tasks

Module ContinuationDemo

    ' 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
    Sub Main()
        Dim action As Action(Of String) = Sub(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)")
        ' Continuations ignore antecedent data
        Task.Factory.StartNew(Sub() action("alpha")).ContinueWith(Sub(antecendent) action("beta")).ContinueWith(Sub(antecendent) action("gamma")).Wait()


        Dim negate As Func(Of Integer, Integer) = Function(n)
                                                      Console.WriteLine("Task={0}, n={1}, -n={2}, Thread={3}", Task.CurrentId, n, -n, Thread.CurrentThread.ManagedThreadId)
                                                      Return -n
                                                  End Function

        ' Creating a sequence of function tasks where each continuation uses the result from its antecendent
        Console.WriteLine(vbLf & "Creating a sequence of function tasks where each continuation uses the result from its antecendent")
        Task(Of Integer).Factory.StartNew(Function() negate(5)).ContinueWith(Function(antecendent) negate(antecendent.Result)).ContinueWith(Function(antecendent) negate(antecendent.Result)).Wait()


        ' Creating a sequence of tasks where you can mix and match the types
        Console.WriteLine(vbLf & "Creating a sequence of tasks where you can mix and match the types")
        Task(Of Integer).Factory.StartNew(Function() negate(6)).ContinueWith(Sub(antecendent) action("x")).ContinueWith(Function(antecendent) negate(7)).Wait()
    End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:40,代码来源:Task.ContinueWith


注:本文中的System.Threading.Tasks.Task.ContinueWith方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。