本文整理汇总了VB.NET中System.Threading.Tasks.Task类的典型用法代码示例。如果您正苦于以下问题:VB.NET Task类的具体用法?VB.NET Task怎么用?VB.NET Task使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Task类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim action As Action(Of Object) =
Sub(obj As Object)
Console.WriteLine("Task={0}, obj={1}, Thread={2}",
Task.CurrentId, obj,
Thread.CurrentThread.ManagedThreadId)
End Sub
' Construct an unstarted task
Dim t1 As New Task(action, "alpha")
' Construct a started task
Dim t2 As Task = Task.Factory.StartNew(action, "beta")
' Block the main thread to demonstrate that t2 is executing
t2.Wait()
' Launch t1
t1.Start()
Console.WriteLine("t1 has been launched. (Main Thread={0})",
Thread.CurrentThread.ManagedThreadId)
' Wait for the task to finish.
t1.Wait()
' Construct a started task using Task.Run.
Dim taskData As String = "delta"
Dim t3 As Task = Task.Run(Sub()
Console.WriteLine("Task={0}, obj={1}, Thread={2}",
Task.CurrentId, taskData,
Thread.CurrentThread.ManagedThreadId)
End Sub)
' Wait for the task to finish.
t3.Wait()
' Construct an unstarted task
Dim t4 As New Task(action, "gamma")
' Run it synchronously
t4.RunSynchronously()
' Although the task was run synchronously, it is a good practice
' to wait for it in the event exceptions were thrown by the task.
t4.Wait()
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:47,代码来源:Task 输出:
Task=1, obj=beta, Thread=3
t1 has been launched. (Main Thread=1)
Task=2, obj=alpha, Thread=3
Task=3, obj=delta, Thread=3
Task=4, obj=gamma, Thread=1
示例2: Example
' 导入命名空间
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim t As Task = Task.Run(Sub()
' Just loop.
Dim ctr As Integer = 0
For ctr = 0 to 1000000
Next
Console.WriteLine("Finished {0} loop iterations",
ctr)
End Sub)
t.Wait()
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:16,代码来源:Task 输出:
Finished 1000001 loop iterations
示例3: Example
' 导入命名空间
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim t As Task = Task.Factory.StartNew(Sub()
' Just loop.
Dim ctr As Integer = 0
For ctr = 0 to 1000000
Next
Console.WriteLine("Finished {0} loop iterations",
ctr)
End Sub)
t.Wait()
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:16,代码来源:Task 输出:
Finished 1000001 loop iterations
示例4: Example
' 导入命名空间
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
' Wait on a single task with no timeout specified.
Dim taskA = Task.Run( Sub() Thread.Sleep(2000))
Console.WriteLine("taskA Status: {0}", taskA.Status)
Try
taskA.Wait()
Console.WriteLine("taskA Status: {0}", taskA.Status)
Catch e As AggregateException
Console.WriteLine("Exception in taskA.")
End Try
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:17,代码来源:Task 输出:
taskA Status: WaitingToRun
taskA Status: RanToCompletion
示例5: Example
' 导入命名空间
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
' Wait on a single task with a timeout specified.
Dim taskA As Task = Task.Run( Sub() Thread.Sleep(2000))
Try
taskA.Wait(1000) ' Wait for 1 second.
Dim completed As Boolean = taskA.IsCompleted
Console.WriteLine("Task.Completed: {0}, Status: {1}",
completed, taskA.Status)
If Not completed Then
Console.WriteLine("Timed out before task A completed.")
End If
Catch e As AggregateException
Console.WriteLine("Exception in taskA.")
End Try
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:21,代码来源:Task 输出:
Task A completed: False, Status: Running
Timed out before task A completed.
示例6: Example
' 导入命名空间
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim tasks(2) As Task
Dim rnd As New Random()
For ctr As Integer = 0 To 2
tasks(ctr) = Task.Run( Sub() Thread.Sleep(rnd.Next(500, 3000)))
Next
Try
Dim index As Integer= Task.WaitAny(tasks)
Console.WriteLine("Task #{0} completed first.", tasks(index).Id)
Console.WriteLine()
Console.WriteLine("Status of all tasks:")
For Each t in tasks
Console.WriteLine(" Task #{0}: {1}", t.Id, t.Status)
Next
Catch e As AggregateException
Console.WriteLine("An exception occurred.")
End Try
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:25,代码来源:Task 输出:
Task #1 completed first.
Status of all tasks:
Task #3: Running
Task #1: RanToCompletion
Task #4: Running
示例7: Example
' 导入命名空间
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
' Wait for all tasks to complete.
Dim tasks(9) As Task
For i As Integer = 0 To 9
tasks(i) = Task.Run( Sub() Thread.Sleep(2000) )
Next
Try
Task.WaitAll(tasks)
Catch ae As AggregateException
Console.WriteLine("One or more exceptions occurred: ")
For Each ex In ae.Flatten().InnerExceptions
Console.WriteLine(" {0}", ex.Message)
Next
End Try
Console.WriteLine("Status of completed tasks:")
For Each t in tasks
Console.WriteLine(" Task #{0}: {1}", t.Id, t.Status)
Next
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:26,代码来源:Task 输出:
Status of completed tasks:
Task #2: RanToCompletion
Task #1: RanToCompletion
Task #3: RanToCompletion
Task #4: RanToCompletion
Task #6: RanToCompletion
Task #5: RanToCompletion
Task #7: RanToCompletion
Task #8: RanToCompletion
Task #9: RanToCompletion
Task #10: RanToCompletion
示例8: Example
' 导入命名空间
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
' Create a cancellation token and cancel it.
Dim source1 As New CancellationTokenSource()
Dim token1 As CancellationToken = source1.Token
source1.Cancel()
' Create a cancellation token for later cancellation.
Dim source2 As New CancellationTokenSource()
Dim token2 As CancellationToken = source2.Token
' Create a series of tasks that will complete, be cancelled,
' timeout, or throw an exception.
Dim tasks(11) As Task
For i As Integer = 0 To 11
Select Case i Mod 4
' Task should run to completion.
Case 0
tasks(i) = Task.Run( Sub() Thread.Sleep(2000))
' Task should be set to canceled state.
Case 1
tasks(i) = Task.Run( Sub() Thread.Sleep(2000), token1)
Case 2
' Task should throw an exception.
tasks(i) = Task.Run( Sub()
Throw New NotSupportedException()
End Sub)
Case 3
' Task should examine cancellation token.
tasks(i) = Task.Run( Sub()
Thread.Sleep(2000)
If token2.IsCancellationRequested
token2.ThrowIfCancellationRequested()
End If
Thread.Sleep(500)
End Sub, token2)
End Select
Next
Thread.Sleep(250)
source2.Cancel()
Try
Task.WaitAll(tasks)
Catch ae As AggregateException
Console.WriteLine("One or more exceptions occurred:")
For Each ex in ae.InnerExceptions
Console.WriteLine(" {0}: {1}", ex.GetType().Name, ex.Message)
Next
End Try
Console.WriteLine()
Console.WriteLine("Status of tasks:")
For Each t in tasks
Console.WriteLine(" Task #{0}: {1}", t.Id, t.Status)
If t.Exception IsNot Nothing Then
For Each ex in t.Exception.InnerExceptions
Console.WriteLine(" {0}: {1}", ex.GetType().Name,
ex.Message)
Next
End If
Next
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:66,代码来源:Task 输出:
One or more exceptions occurred:
TaskCanceledException: A task was canceled.
NotSupportedException: Specified method is not supported.
TaskCanceledException: A task was canceled.
TaskCanceledException: A task was canceled.
NotSupportedException: Specified method is not supported.
TaskCanceledException: A task was canceled.
TaskCanceledException: A task was canceled.
NotSupportedException: Specified method is not supported.
TaskCanceledException: A task was canceled.
Status of tasks:
Task #13: RanToCompletion
Task #1: Canceled
Task #3: Faulted
NotSupportedException: Specified method is not supported.
Task #8: Canceled
Task #14: RanToCompletion
Task #4: Canceled
Task #6: Faulted
NotSupportedException: Specified method is not supported.
Task #7: Canceled
Task #15: RanToCompletion
Task #9: Canceled
Task #11: Faulted
NotSupportedException: Specified method is not supported.
Task #12: Canceled
注:本文中的System.Threading.Tasks.Task类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论