Image for post
Image for post

Executing async code from non async code

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

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

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.

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store