TaskEx.Run是自定义的方法,为了使.net4.0也支持.net4.5的 task run语法。
代码未经测试
Class TaskEx '4.0模拟 task.run Public Shared Function Run(ByVal action As Action) As Task Dim tcs = New TaskCompletionSource(Of Object)() Dim t As New Thread(Sub() Try action() tcs.SetResult(Nothing) Catch ex As Exception tcs.SetException(ex) End Try End Sub) With {.IsBackground = True} t.Start() Return tcs.Task End Function Public Shared Function Run(Of TResult)(ByVal [function] As Func(Of TResult)) As Task(Of TResult) Dim tcs = New TaskCompletionSource(Of TResult)() Dim t As New Thread(Sub() Try tcs.SetResult([function]()) Catch ex As Exception tcs.SetException(ex) End Try End Sub) With {.IsBackground = True} t.Start() Return tcs.Task End Function Public Shared Function Delay(ByVal milliseconds As Integer) As Task Dim tcs = New TaskCompletionSource(Of Object)() Dim timer = New System.Timers.Timer(milliseconds) With { .AutoReset = False } AddHandler timer.Elapsed, Sub() timer.Dispose() tcs.SetResult(Nothing) End Sub timer.Start() Return tcs.Task End Function End Class
标签:4.5,Dim,vb,task,End,Sub,tcs,New,Function From: https://www.cnblogs.com/MadeInChinese/p/17986415