Executing async code from non async code

Dominic Burford
3 min readJul 20, 2020

--

Whilst developing the journey logging functionality for our Xamarin mobile app, we needed to give the user the ability to start and stop the app from recording their journey. The functionality was designed to give drivers the ability to record their business journeys so that they could reclaim business mileage expenses. So when they set off on their business journey, they would open the app and set it to begin recording their journey. When they arrived at their destination they would stop the app from recording their journey. For anyone who’s ever used a running / cycling app such as Garmin or Strava, the concept is identical.

We already had the functionality for initialising and recording the driver’s journey. These were RESTful services that send the data to our backend for storage and processing. The problem came when we tried to plug these RESTful services into the Xamarin app. The Xamarin service methods we wanted to plug into were not async. They were overridden service methods, and we had no control over these whatsoever. Our RESTful services for initialising and recording the journey were both async.

So the question was how to execute async code from inside non async code. Turns out the solution is pretty straight forward. The .NET Framework has already solved this problem, as there are many use cases where async code will need to be run from inside non async code. As developers we won’t always have full control over the entire pipeline of a process. Sometimes we will be constrained by the underlying environment (in our case Xamarin service methods), or we may be updating existing non async code with new async functionality. Either way, there will be times when we need to run async code from non async code.

Below is the OnStartCommand from the Xamarin service. We have no control over this method and cannot therefore make it async.

Hide Copy Code

public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
//how do we execute async code from inside this method?
}

The Task object contains a method for doing exactly this. The Run() method contains several overloads to allow for the execution of tasks, including async tasks. The Task.Run() method queues a task to run on the threadpool and returns a handle to the task.

Here’s the Xamarin OnStartCommand method updated to execute our RESTful services for initialising and recording a journey.

Hide Copy Code

public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
base.OnStartCommand(intent, flags, startId);
Task.Run(async () => { await this.InitialiseJourney(); });

Task.Run(async () => { await this.StartJourneyTracking(); });
return StartCommandResult.NotSticky;
}

This is just one simple example of using the Task.Run() method. It can be used in many different ways to solve many similar problems. By giving the developer the ability to queue and execute tasks, this is a very powerful method that is worth understanding. If you’re writing async code you should definitely check out this method.

--

--

Dominic Burford

A father, cyclist, vegetarian, atheist, geek and multiple award winning technical author. Loves real ale, fine wine and good music. All round decent chap.