本文整理匯總了VB.NET中System.Threading.Tasks.Task.Status屬性的典型用法代碼示例。如果您正苦於以下問題:VB.NET Task.Status屬性的具體用法?VB.NET Task.Status怎麽用?VB.NET Task.Status使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類System.Threading.Tasks.Task
的用法示例。
在下文中一共展示了Task.Status屬性的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
輸出:
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