本文整理汇总了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