Asynchronous Programming

Overview

If you need to run code that takes an unpredictable amount of time to finish, use asynchronous programming to run the operation in a separate thread and monitor the task to check when it completes.

Important

It seems that if you choose to add async functions to your scripts, the best practice is to only call them using other async functions. Unity allows users to add the async keyword to event functions like Awake Start and OnEnable. So you can call your async methods from there.

When Does The Body Of An Asynchronous Function Run?

  • An async method runs synchronously (the function that called the async method waits) until it reaches its first await expression. Then it is suspended until the awaited task is complete. In the meantime control returns to the caller of the method.

  • (As I understand it) In unity you can call await Task.Yield() to essentially suspend an async method for at minimum one frame. (As I understand it) This is made possible through Unity’s UnitySynchronizationContext since by default async / await logic relies on the default SynchronizationContext which has no concept of frames in unity.

When Does an Asynchronous Function Stop?

  • Async functions only stop once they execute through all of their code (run to completion).

  • Async functions in scripts attached to objects are not tied to a GameObject and will continue to run even if the object they are attached to is destroyed. This also means that they will continue to run even if a new scene is loaded.

Important

  • Unity warns us that async tasks are not automatically stopped when exiting play mode!!! So if you have an async function that loops infinitely / never quits you may be stuck with it running while in the editor.

  • If this happens to you. To cancel all async tasks being run outside of play mode, simply enter play mode again. You can enter play mode in an empty scene so that you aren’t accidentally calling async functions again in play mode.

How To Cancel Asynchronous Functions In Unity

Note

If you are using unity and are tyring to cancel an async method that is out of your control, simply enter play mode (preferably in an empty scene is best since there is no risk of re executing the functions that got out of control in the first place).

Because asynchronous functions can continue to execute even when loading in new scenes or exiting play mode, It is important to have a way to terminate async functions in case things go wrong and in the worse case they are stuck in an infinite loop. The convention for cancelling async functions appears to be introducing try finally statements into the async function and raising an error when we want to exit early. For raising these errors we use CancellationTokens

See examples below:

Important

While the following examples showcase their functionality, I recommend that every async you use in unity has at least the ability to cancel when the object is destroyed.

Examples

Basic Asynchronous Programming

Working With Multiple Async Functions