窗口商店应用程序具有我需要在应用程序启动时调用的长时间运行方法,但我不需要等待它完成.我希望它作为后台任务运行.如果转到应用程序的某个部分(报告),那么我将检查并在必要时
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
它被称为这样
Dim updateTask As Task(Of Boolean) = UpdateVehicleSummaries(0)
它没有Await调用,我得到它将同步运行的警告.我如何启动这样的东西并让它以异步方式运行?我希望它在自己的线程/任务上运行而不会阻塞接口线程.有任何想法吗?
感谢名单!
你应该在你的函数中运行代码,然后你可以从它返回: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
然后,您可以按照建议调用您的函数:
Dim updateTask As Task(Of Boolean) = UpdateVehicleSummaries(0)
稍后,您可以在需要结果时等待任务完成:
Dim result = Await updateTask