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


VB.NET TaskStatus枚舉代碼示例

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


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

示例1: Example

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

Module Example

   Public Sub Main()
      Dim tasks As New List(Of Task(Of Integer))()
      Dim source As New CancellationTokenSource
      Dim token As CancellationToken = source.Token
      Dim completedIterations As Integer = 0
      
      For n As Integer = 0 To 19
         tasks.Add(Task.Run( Function()
                                Dim iterations As Integer= 0
                                For ctr As Long = 1 To 2000000
                                   token.ThrowIfCancellationRequested()
                                   iterations += 1
                                Next
                                Interlocked.Increment(completedIterations)
                                If completedIterations >= 10 Then source.Cancel()
                                Return iterations
                             End Function, token))
      Next

      Console.WriteLine("Waiting for the first 10 tasks to complete... ")
      Try
         Task.WaitAll(tasks.ToArray())
      Catch e As AggregateException
         Console.WriteLine("Status of tasks:")
         Console.WriteLine()
         Console.WriteLine("{0,10} {1,20} {2,14}", "Task Id",
                           "Status", "Iterations")
         For Each t In tasks
            Console.WriteLine("{0,10} {1,20} {2,14}",
                              t.Id, t.Status,
                              If(t.Status <> TaskStatus.Canceled,
                                 t.Result.ToString("N0"), "n/a"))
         Next
      End Try
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System.Threading.Tasks,代碼行數:43,代碼來源:TaskStatus

輸出:

Waiting for the first 10 tasks to complete...
Status of tasks:

Task Id               Status     Iterations
1      RanToCompletion      2,000,000
2      RanToCompletion      2,000,000
3      RanToCompletion      2,000,000
4      RanToCompletion      2,000,000
5      RanToCompletion      2,000,000
6      RanToCompletion      2,000,000
7      RanToCompletion      2,000,000
8      RanToCompletion      2,000,000
9      RanToCompletion      2,000,000
10             Canceled            n/a
11             Canceled            n/a
12             Canceled            n/a
13             Canceled            n/a
14             Canceled            n/a
15             Canceled            n/a
16      RanToCompletion      2,000,000
17             Canceled            n/a
18             Canceled            n/a
19             Canceled            n/a
20             Canceled            n/a


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