當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET TaskFactory.ContinueWhenAny方法代碼示例

本文整理匯總了VB.NET中System.Threading.Tasks.TaskFactory.ContinueWhenAny方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET TaskFactory.ContinueWhenAny方法的具體用法?VB.NET TaskFactory.ContinueWhenAny怎麽用?VB.NET TaskFactory.ContinueWhenAny使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Threading.Tasks.TaskFactory的用法示例。


在下文中一共展示了TaskFactory.ContinueWhenAny方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: ContinuationWhenMulti

' 導入命名空間
Imports System.Threading
Imports System.Threading.Tasks

Module ContinuationWhenMulti
    ' Demonstrated features:
    '   Task.Factory
    '   TaskFactory.ContinueWhenAll()
    '   TaskFactory.ContinueWhenAny()
    '   Task.Wait()
    ' Expected results:
    '   Three tasks are created in parallel. 
    '   Each task for a different period of time prints a number and returns it.
    '   A ContinueWhenAny() task indicates the first of the three tasks to complete.
    '   A ContinueWhenAll() task sums up the results of the three tasks and prints out the total.
    ' Documentation:
    '   http://msdn.microsoft.com/library/system.threading.tasks.taskfactory_members(VS.100).aspx
    Sub Main()
        ' Schedule a list of tasks that return integer
        Dim tasks As Task(Of Integer)() = New Task(Of Integer)() {
            Task(Of Integer).Factory.StartNew(Function()
                                                  Thread.Sleep(500)
                                                  Console.WriteLine("Task={0}, Thread={1}, x=5", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
                                                  Return 5
                                              End Function),
            Task(Of Integer).Factory.StartNew(Function()
                                                  Thread.Sleep(10)
                                                  Console.WriteLine("Task={0}, Thread={1}, x=3", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
                                                  Return 3
                                              End Function),
            Task(Of Integer).Factory.StartNew(Function()
                                                  Thread.Sleep(200)
                                                  Console.WriteLine("Task={0}, Thread={1}, x=2", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
                                                  Return 2
                                              End Function)}


        ' Schedule a continuation to indicate the result of the first task to complete
        Task.Factory.ContinueWhenAny(tasks, Sub(winner)
                                                ' You would expect winning result = 3 on multi-core systems, because you expect
                                                ' tasks[1] to finish first.
                                                Console.WriteLine("Task={0}, Thread={1} (ContinueWhenAny): Winning result = {2}", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, winner.Result)
                                            End Sub)


        ' Schedule a continuation that sums up the results of all tasks, then wait on it.
        ' The list of antecendent tasks is passed as an argument by the runtime.
        Task.Factory.ContinueWhenAll(tasks, Sub(antecendents)
                                                Dim sum As Integer = 0
                                                For Each task__1 As Task(Of Integer) In antecendents
                                                    sum += task__1.Result
                                                Next
                                                Console.WriteLine("Task={0}, Thread={1}, (ContinueWhenAll): Total={2} (expected 10)", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, sum)
                                            End Sub).Wait()
    End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System.Threading.Tasks,代碼行數:56,代碼來源:TaskFactory.ContinueWhenAny


注:本文中的System.Threading.Tasks.TaskFactory.ContinueWhenAny方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。