natinusala / borealis

Hardware accelerated, controller and TV oriented UI library for PC and Nintendo Switch (libnx)
Apache License 2.0
257 stars 81 forks source link

Asynchronous background task #119

Open natinusala opened 3 years ago

natinusala commented 3 years ago

Currently, the borealis code runs on a single thread. Users can do asynchronous work on a separate thread if they wish, but they have to synchronize the operation manually with the main thread (or "UI thread").

The point of an "asynchronous background task" is to provide a way to automatically synchronize the background work with the main thread, similarly to the (late) AsyncTask class of Android. The design is actually copy pasted from AsyncTask 🙈

The task can be implemented as a template class with three arguments:

The class contains multiple methods, all of them synchronized:

When you start the task:

  1. onPreExecute is called
  2. run is posted on a new thread
  3. run repeatedly calls reportProgress

Meanwhile, the UI thread checks the state of the task every frame (with synchronization):