本文整理汇总了VB.NET中System.Threading.Tasks.TaskExtensions.Unwrap方法的典型用法代码示例。如果您正苦于以下问题:VB.NET TaskExtensions.Unwrap方法的具体用法?VB.NET TaskExtensions.Unwrap怎么用?VB.NET TaskExtensions.Unwrap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了TaskExtensions.Unwrap方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: UnwrapDemo
' 导入命名空间
Imports System.Threading
Imports System.Threading.Tasks
Module UnwrapDemo
' Demonstrated features:
' Task.Unwrap()
' Task.Factory.StartNew()
' Task.ContinueWith()
' Expected results:
' Indicates that continuation chains can be set up virtually instantaneously using Unwrap(), and then left to run on their own.
' The results of the RemoteIncrement(0) chain and the RemoteIncrement(4) chain may be intermixed with each other.
' The results of the sequence that starts with RemoteIncrement(4) are in strict order.
' Documentation:
' http://msdn.microsoft.com/library/dd781129(VS.100).aspx
' More information:
' http://blogs.msdn.com/pfxteam/archive/2009/11/04/9917581.aspx
' Other notes:
' The combination of Task<T>, ContinueWith() and Unwrap() can be particularly useful for setting up a chain of long-running
' tasks where each task uses the results of its predecessor.
Sub Main()
' Invoking individual tasks is straightforward
Dim t1 As Task(Of Integer) = RemoteIncrement(0)
Console.WriteLine("Started RemoteIncrement(0)")
' Chain together the results of (simulated) remote operations.
' The use of Unwrap() instead of .Result below prevents this thread from blocking while setting up this continuation chain.
' RemoteIncrement() returns Task<int> so no unwrapping is needed for the first continuation.
' ContinueWith() here returns Task<Task<int>>. Therefore unwrapping is needed.
' and on it goes...
Dim t2 As Task(Of Integer) = RemoteIncrement(4).ContinueWith(Function(t) RemoteIncrement(t.Result)).Unwrap().ContinueWith(Function(t) RemoteIncrement(t.Result)).Unwrap().ContinueWith(Function(t) RemoteIncrement(t.Result)).Unwrap()
Console.WriteLine("Started RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...)")
Try
t1.Wait()
Console.WriteLine("Finished RemoteIncrement(0)" & vbLf)
t2.Wait()
Console.WriteLine("Finished RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...)")
Catch e As AggregateException
Console.WriteLine("A task has thrown the following (unexpected) exception:" & vbLf & "{0}", e)
End Try
End Sub
' This method represents a remote API.
Function RemoteIncrement(ByVal n As Integer) As Task(Of Integer)
Return Task(Of Integer).Factory.StartNew(Function(obj)
' Simulate a slow operation
Thread.Sleep(1 * 1000)
Dim x As Integer = CInt(obj)
Console.WriteLine("Thread={0}, Next={1}", Thread.CurrentThread.ManagedThreadId, System.Threading.Interlocked.Increment(x))
Return x
End Function, n)
End Function
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:60,代码来源:TaskExtensions.Unwrap
注:本文中的System.Threading.Tasks.TaskExtensions.Unwrap方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论