I would like to run a task inside a method and return the value that the Task produces, how I can do it?
我想在方法中运行一个任务并返回Task生成的值,我该怎么做?
This is what I'm trying:
这就是我正在尝试的:
Private function blahblahblah as boolean
Return Threading.Tasks.Task.Factory.StartNew(Of Boolean)(
Function() As Boolean
For X As Integer = 0 To RetryInterval
NetworkAvaliable(String.Format(NoWebsiteRetryMessage, RetryInterval - X), True, StatusControl)
Next
Return False
End Function)
End Function
It is important to clarify that I want to do it without creating other variables outside the method like for example something like this:
重要的是澄清我想要在不创建方法之外的其他变量的情况下这样做,例如:
dim value as boolean
Private function blahblahblah as boolean
Threading.Tasks.Task.Factory.StartNew(Of Boolean)(
Function() As Boolean
For X As Integer = 0 To RetryInterval
NetworkAvaliable(String.Format(NoWebsiteRetryMessage, RetryInterval - X), True, StatusControl)
Next
value = False
End Function)
Return value
End Function
I want to avoid things like that.
我想避免这样的事情。
1 个解决方案
#1
3
Use Async / Await
使用Async / Await
Private Async Sub callBlahBlahBlah()
Dim result = Await blahblahblah()
End Sub
Private Function blahblahblah() As Task(Of Boolean)
Return Threading.Tasks.Task.Factory.StartNew(Of Boolean)(
Function() As Boolean
For X As Integer = 0 To RetryInterval
NetworkAvaliable(String.Format(NoWebsiteRetryMessage, RetryInterval - X), True, StatusControl)
Next
Return False
End Function)
End Function
#1
3
Use Async / Await
使用Async / Await
Private Async Sub callBlahBlahBlah()
Dim result = Await blahblahblah()
End Sub
Private Function blahblahblah() As Task(Of Boolean)
Return Threading.Tasks.Task.Factory.StartNew(Of Boolean)(
Function() As Boolean
For X As Integer = 0 To RetryInterval
NetworkAvaliable(String.Format(NoWebsiteRetryMessage, RetryInterval - X), True, StatusControl)
Next
Return False
End Function)
End Function