New async/await features

We have improved the async/await support in WaveEngine 2.4.0 by replacing our Dispatcher and TaskScheduler services with a custom implementation of .Net TaskScheduler and creating some useful helpers.

Porting project

The biggest change is the substitution of Dispatcher and TaskScheduler services. The new way to execute code in the foreground or background threads is by using the new helper methods from WaveForegroundTask and WaveBackgroundTask static classes.

To port current projects using the new classes, we need to change the following:

  • If you are using WaveServices.Dispatcher, you must replace it to use WaveForegroundTask.

  • If you are using WaveServices.TaskScheduler, you must replace it to use WaveBackgroundTask.

Both new ways to execute code return a .Net Task, which integrates easily with async .Net code.

Foreground and Background threads

Before we start to review the new included features, it is good to know these details:

  • When a task is executed in the Foreground thread, like with the Dispatcher service, it is executed in the next update cycle.
  • With the Background thread, the tasks are executed sequentially in a specific thread that can access the graphic resources. This allows us, for example, to load textures. The old TaskScheduler executes the code in a .Net thread and without taking concurrency issues into account when accessing the graphic context. In comparison, this new service ensures that each task is executed in the same thread and sequentially.


One useful helper introduced in the new WaveEngine version is the ConfigureWaveAwait extension method.
It allows us to configure how the await continuation is handled. Just as the .Net ConfigureAwait extension method allows us to configure if we want that the continuation of the await is executed on the captured context, the method ConfigureWaveAwait allows us to configure where the await continuation is executed: in the Foreground or the Background thread.

A simple way to test this new method is to load an image from a URL into a sprite:

The WaveTaskContinueOn enum also includes other options. The Default option acts as if the ConfigureWaveAwait has not been used, and the ThreadPool option acts as if the ConfigureAwait(false) from .Net has been used.

Async/await and GameActions

With the inclusion of GameActions, WaveEngine has acquired an excellent way to define a flow of actions that is integrated perfectly with the common scenarios in a game (like animations, play sounds, wait for a condition, etc.).

In this release, we have included the AsTask extension method that simplifies the way that .Net Tasks and GameAction interact with each other.

A simple sample using all of these new features:

I hope you find it useful when you want to use async code in your game. It is the first step and we will work to include new async APIs and features in next releases.