本文整理汇总了VB.NET中System.Threading.Tasks.TaskCompletionSource<TResult>类的典型用法代码示例。如果您正苦于以下问题:VB.NET TaskCompletionSource<TResult>类的具体用法?VB.NET TaskCompletionSource<TResult>怎么用?VB.NET TaskCompletionSource<TResult>使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TaskCompletionSource<TResult>类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: TCSDemo
' 导入命名空间
Imports System.Diagnostics
Imports System.Threading
Imports System.Threading.Tasks
Module TCSDemo
' Demonstrated features:
' TaskCompletionSource ctor()
' TaskCompletionSource.SetResult()
' TaskCompletionSource.SetException()
' Task.Result
' Expected results:
' The attempt to get t1.Result blocks for ~1000ms until tcs1 gets signaled. 15 is printed out.
' The attempt to get t2.Result blocks for ~1000ms until tcs2 gets signaled. An exception is printed out.
Private Sub Main()
Dim tcs1 As New TaskCompletionSource(Of Integer)()
Dim t1 As Task(Of Integer) = tcs1.Task
' Start a background task that will complete tcs1.Task
Task.Factory.StartNew(Sub()
Thread.Sleep(1000)
tcs1.SetResult(15)
End Sub)
' The attempt to get the result of t1 blocks the current thread until the completion source gets signaled.
' It should be a wait of ~1000 ms.
Dim sw As Stopwatch = Stopwatch.StartNew()
Dim result As Integer = t1.Result
sw.Stop()
Console.WriteLine("(ElapsedTime={0}): t1.Result={1} (expected 15) ", sw.ElapsedMilliseconds, result)
' ------------------------------------------------------------------
' Alternatively, an exception can be manually set on a TaskCompletionSource.Task
Dim tcs2 As New TaskCompletionSource(Of Integer)()
Dim t2 As Task(Of Integer) = tcs2.Task
' Start a background Task that will complete tcs2.Task with an exception
Task.Factory.StartNew(Sub()
Thread.Sleep(1000)
tcs2.SetException(New InvalidOperationException("SIMULATED EXCEPTION"))
End Sub)
' The attempt to get the result of t2 blocks the current thread until the completion source gets signaled with either a result or an exception.
' In either case it should be a wait of ~1000 ms.
sw = Stopwatch.StartNew()
Try
result = t2.Result
Console.WriteLine("t2.Result succeeded. THIS WAS NOT EXPECTED.")
Catch e As AggregateException
Console.Write("(ElapsedTime={0}): ", sw.ElapsedMilliseconds)
Console.WriteLine("The following exceptions have been thrown by t2.Result: (THIS WAS EXPECTED)")
For j As Integer = 0 To e.InnerExceptions.Count - 1
Console.WriteLine(vbLf & "-------------------------------------------------" & vbLf & "{0}", e.InnerExceptions(j).ToString())
Next
End Try
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:62,代码来源:TaskCompletionSource
注:本文中的System.Threading.Tasks.TaskCompletionSource<TResult>类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论