本文整理汇总了VB.NET中System.Threading.Tasks.Task<TResult>.ContinueWith方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Task<TResult>.ContinueWith方法的具体用法?VB.NET Task<TResult>.ContinueWith怎么用?VB.NET Task<TResult>.ContinueWith使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Tasks.Task<TResult> 的用法示例。
在下文中一共展示了Task<TResult>.ContinueWith方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks
Imports Timers = System.Timers
Module Example
Dim ts As CancellationTokenSource
Public Sub Main(args() As String)
Dim upperBound As Integer = If(args.Length >= 1, CInt(args(0)), 200)
ts = New CancellationTokenSource()
Dim token As CancellationToken = ts.Token
Dim timer As New Timers.Timer(100)
AddHandler timer.Elapsed, AddressOf TimedOutEvent
timer.AutoReset = False
timer.Enabled = True
Dim t1 = Task.Run(Function()
' True = composite.
' False = prime.
Dim values(upperBound) As Boolean
For ctr = 2 To CInt(Math.Sqrt(upperBound))
If values(ctr) = False Then
For product = ctr * ctr To upperBound Step ctr
values(product) = True
Next
End If
token.ThrowIfCancellationRequested()
Next
Return values
End Function, token)
Dim t2 = t1.ContinueWith(Sub(antecedent)
' Create a list of prime numbers.
Dim primes As New List(Of Integer)()
token.ThrowIfCancellationRequested()
Dim numbers As Boolean() = antecedent.Result
Dim output As String = String.Empty
For ctr As Integer = 1 To numbers.GetUpperBound(0)
If numbers(ctr) = False Then primes.Add(ctr)
Next
' Create the output string.
For ctr As Integer = 0 To primes.Count - 1
token.ThrowIfCancellationRequested()
output += primes(ctr).ToString("N0")
If ctr < primes.Count - 1 Then output += ", "
If (ctr + 1) Mod 8 = 0 Then output += vbCrLf
Next
'Display the result.
Console.WriteLine("Prime numbers from 1 to {0}:{1}",
upperBound, vbCrLf)
Console.WriteLine(output)
End Sub, token)
Try
t2.Wait()
Catch ae As AggregateException
For Each e In ae.InnerExceptions
If e.GetType Is GetType(TaskCanceledException) Then
Console.WriteLine("The operation was cancelled.")
Else
Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message)
End If
Next
Finally
ts.Dispose()
End Try
End Sub
Private Sub TimedOutEvent(source As Object, e As Timers.ElapsedEventArgs)
ts.Cancel()
End Sub
End Module
' If cancellation is not requested, the example displays output like the following:
' Prime numbers from 1 to 400:
'
' 1, 2, 3, 5, 7, 11, 13, 17,
' 19, 23, 29, 31, 37, 41, 43, 47,
' 53, 59, 61, 67, 71, 73, 79, 83,
' 89, 97, 101, 103, 107, 109, 113, 127,
' 131, 137, 139, 149, 151, 157, 163, 167,
' 173, 179, 181, 191, 193, 197, 199, 211,
' 223, 227, 229, 233, 239, 241, 251, 257,
' 263, 269, 271, 277, 281, 283, 293, 307,
' 311, 313, 317, 331, 337, 347, 349, 353,
' 359, 367, 373, 379, 383, 389, 397, 401
' If cancellation is requested, the example displays output like the following:
' The operation was cancelled.
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:90,代码来源:Task.ContinueWith
示例2: Example
' 导入命名空间
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim cts As New CancellationTokenSource()
Dim token As CancellationToken = cts.Token
' Get an integer to generate a list of its exponents.
Dim rnd As New Random()
Dim number As Integer = rnd.Next(2, 21)
Dim t = Task.Factory.StartNew( Function(value)
Dim n As Integer = CInt(value)
Dim values(9) As Long
For ctr As Integer = 1 To 10
values(ctr - 1) = CLng(Math.Pow(n, ctr))
Next
return values
End Function, number)
Dim continuation = t.ContinueWith( Sub(antecedent, value)
Console.WriteLine("Exponents of {0}:", value)
For ctr As Integer = 0 To 9
Console.WriteLine(" {0} {1} {2} = {3:N0}",
value, ChrW(&h02C6), ctr + 1,
antecedent.Result(ctr))
Next
Console.WriteLine()
End Sub, number)
continuation.Wait()
cts.Dispose()
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:35,代码来源:Task.ContinueWith 输出:
Exponents of 2:
2 ^ 1 = 2
2 ^ 2 = 4
2 ^ 3 = 8
2 ^ 4 = 16
2 ^ 5 = 32
2 ^ 6 = 64
2 ^ 7 = 128
2 ^ 8 = 256
2 ^ 9 = 512
2 ^ 10 = 1,024
示例3: Example
' 导入命名空间
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main(args() As String)
Dim upperBound As Integer = If(args.Length >= 1, CInt(args(0)), 200)
Dim t1 = Task.Run(Function()
' True = composite.
' False = prime.
Dim values(upperBound) As Boolean
For ctr = 2 To CInt(Math.Sqrt(upperBound))
If values(ctr) = False Then
For product = ctr * ctr To upperBound Step ctr
values(product) = True
Next
End If
Next
Return values
End Function)
Dim t2 = t1.ContinueWith(Sub(antecedent)
' Create a list of prime numbers.
Dim primes As New List(Of Integer)()
Dim numbers As Boolean() = antecedent.Result
Dim output As String = String.Empty
For ctr As Integer = 1 To numbers.GetUpperBound(0)
If numbers(ctr) = False Then primes.Add(ctr)
Next
' Create the output string.
For ctr As Integer = 0 To primes.Count - 1
output += primes(ctr).ToString("N0")
If ctr < primes.Count - 1 Then output += ", "
If (ctr + 1) Mod 8 = 0 Then output += vbCrLf
Next
'Display the result.
Console.WriteLine("Prime numbers from 1 to {0}:{1}",
upperBound, vbCrLf)
Console.WriteLine(output)
End Sub)
Try
t2.Wait()
Catch ae As AggregateException
For Each e In ae.InnerExceptions
Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message)
Next
End Try
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:53,代码来源:Task.ContinueWith 输出:
Prime numbers from 1 to 400:
1, 2, 3, 5, 7, 11, 13, 17,
19, 23, 29, 31, 37, 41, 43, 47,
53, 59, 61, 67, 71, 73, 79, 83,
89, 97, 101, 103, 107, 109, 113, 127,
131, 137, 139, 149, 151, 157, 163, 167,
173, 179, 181, 191, 193, 197, 199, 211,
223, 227, 229, 233, 239, 241, 251, 257,
263, 269, 271, 277, 281, 283, 293, 307,
311, 313, 317, 331, 337, 347, 349, 353,
359, 367, 373, 379, 383, 389, 397, 401
If cancellation is requested, the example displays output like the following:
The operation was cancelled.
示例4: ContinuationState
' 导入命名空间
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks
' Demonstrates how to associate state with task continuations.
Public Module ContinuationState
' Simluates a lengthy operation and returns the time at which
' the operation completed.
Public Function DoWork() As Date
' Simulate work by suspending the current thread
' for two seconds.
Thread.Sleep(2000)
' Return the current time.
Return Date.Now
End Function
Public Sub Main()
' Start a root task that performs work.
Dim t As Task(Of Date) = Task(Of Date).Run(Function() DoWork())
' Create a chain of continuation tasks, where each task is
' followed by another task that performs work.
Dim continuations As New List(Of Task(Of DateTime))()
For i As Integer = 0 To 4
' Provide the current time as the state of the continuation.
t = t.ContinueWith(Function(antecedent, state) DoWork(), DateTime.Now)
continuations.Add(t)
Next
' Wait for the last task in the chain to complete.
t.Wait()
' Display the creation time of each continuation (the state object)
' and the completion time (the result of that task) to the console.
For Each continuation In continuations
Dim start As DateTime = CDate(continuation.AsyncState)
Dim [end] As DateTime = continuation.Result
Console.WriteLine("Task was created at {0} and finished at {1}.",
start.TimeOfDay, [end].TimeOfDay)
Next
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:45,代码来源:Task.ContinueWith 输出:
Task was created at 10:56:21.1561762 and finished at 10:56:25.1672062.
Task was created at 10:56:21.1610677 and finished at 10:56:27.1707646.
Task was created at 10:56:21.1610677 and finished at 10:56:29.1743230.
Task was created at 10:56:21.1610677 and finished at 10:56:31.1779883.
Task was created at 10:56:21.1610677 and finished at 10:56:33.1837083.
注:本文中的System.Threading.Tasks.Task<TResult>.ContinueWith方法示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论