Start Async task without using Await in called method

A window storage app that has a long method that I need to call when the app starts, but I don't need to wait for it to complete. I want it to run as a background task. If the transition to a certain part of the application (reporting), then I check and, if necessary, wait for tasks.

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's called like this

Dim updateTask As Task(Of Boolean) = UpdateVehicleSummaries(0)

      

There is no Await call in it and I am getting a warning that it will run synchronously. How do I start something like this and run it asynchronously? I want it to run on its own thread / task without blocking the interface thread. Any ideas?

Thanx!

+3


source to share


2 answers


You have to run the code in your function inside Task

that you can return from it:

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

      

Then you can call your function as you intended:



Dim updateTask As Task(Of Boolean) = UpdateVehicleSummaries(0)

      

Later, you can wait for the task to complete when you need the result:

Dim result = Await updateTask

      

+10


source


I think this is an even simpler 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 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

      

+2


source







All Articles