vb.net 在调用的方法中不使用 Await 启动异步任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14888984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Start Async Task without using Await in called method
提问by Robert Beaubien
Window store app that has a long running method that I need to call when the application starts up, but I don't need to wait on it to complete. I want it to run as a background task. If the go to a certain part of the application (reporting), then I will check and if necessary wait on the task.
Window 商店应用程序具有长时间运行的方法,我需要在应用程序启动时调用该方法,但我不需要等待它完成。我希望它作为后台任务运行。如果转到应用程序的某个部分(报告),那么我将检查并在必要时等待任务。
Public Shared Async Function UpdateVehicleSummaries(p_vehicleID As Int32) As Task(Of Boolean)
Dim tempVehicle As Koolsoft.MARS.BusinessObjects.Vehicle
For Each tempVehicle In Vehicles
If p_vehicleID = 0 Or p_vehicleID = tempVehicle.VehicleID Then
UpdateVehicleStats(tempVehicle)
End If
Next
Return True
End Function
It is called like this
是这样叫的
Dim updateTask As Task(Of Boolean) = UpdateVehicleSummaries(0)
It has no Await call in it and I get the warning that it will run synchronously. How do I start something like this and have it run asynchronously? I want it to run on its own thread/task without blocking the interface thread. Any ideas?
它没有 Await 调用,我收到了它将同步运行的警告。我如何开始这样的事情并让它异步运行?我希望它在自己的线程/任务上运行而不阻塞接口线程。有任何想法吗?
Thanx!
谢谢!
回答by Damir Arh
You should run the code in your function inside a Taskwhich you can then return from it:
您应该在 a 中运行您的函数中的代码,Task然后您可以从中返回:
Public Shared Function UpdateVehicleSummaries(p_vehicleID As Int32) As Task(Of Boolean)
Return Task.Factory.StartNew(Of Boolean)(
Function()
Dim tempVehicle As Koolsoft.MARS.BusinessObjects.Vehicle
For Each tempVehicle In Vehicles
If p_vehicleID = 0 Or p_vehicleID = tempVehicle.VehicleID Then
UpdateVehicleStats(tempVehicle)
End If
Next
Return True
End Function)
End Function
You can then call your function as you suggested:
然后您可以按照您的建议调用您的函数:
Dim updateTask As Task(Of Boolean) = UpdateVehicleSummaries(0)
Later on you can await the task to complete when you need the result:
稍后您可以在需要结果时等待任务完成:
Dim result = Await updateTask
回答by LorneCash
I think this is an even more simple solution that is much easier to read
我认为这是一个更简单的解决方案,更容易阅读
Public Shared RunningTask As Task
Public Shared Sub CallingSub()
'No need for sub to be async but can be async if needed for other reasons (see below)
'but that doesn't stop the execution in the CallingSub
'Get the task started... it's not going to wait for the task to be done just sets the task variable
RunningTask = UpdateVehicleSummaries(0)
'if a parameter comes from a function it may look like this
'note that if you do this then the CallingSub would need the async keyword
RunningTask = UpdateVehicleSummaries(Await FunctionReturnInt32())
'Do stuff that does not require task to be done
End Sub
Public Shared Async Sub UseTaskResults()
'Must be async so you can await the result
Await RunningTask
'Do stuff that requires task to be done
End Sub
Public Shared Function UpdateVehicleSummaries(p_vehicleID As Int32) As Task
'No need for this function to be async since the function has no await
Dim tempVehicle As Koolsoft.MARS.BusinessObjects.Vehicle
For Each tempVehicle In Vehicles
If p_vehicleID = 0 Or p_vehicleID = tempVehicle.VehicleID Then
UpdateVehicleStats(tempVehicle)
End If
Next
End Function

