Vixen
dd8b0a81
csharp
public sealed class BackgroundTaskManager

The long operations an application is running, and what they have got to.

Read the guide page for this →

Remarks

Never a modal progress dialog — doc 11 — so this is a list, a status bar and a panel. An import that takes forty seconds must not stop somebody opening a different file while it runs, and an application whose long operations block it is one where every long operation has to be made short.

⚠ Something has to call Pump once a frame or nothing here ever changes. UiApplication does it for an application that uses the standard loop and exposes the manager it drives as UiApplication.Tasks; a host with its own loop owns a manager and pumps it itself, which is what the editor's shell does. A manager nobody pumps is a list of tasks stuck at nought per cent — the reported numbers are queued, not applied, so this fails silently rather than loudly.

⚠ Every mutation lands on the UI thread, in Pump. Work runs wherever the caller put it; what it reports is queued and applied at one point in the frame. This is the whole of the threading design and it is deliberately the smallest one that works: no locks around the task list, no concurrent collection for the UI to walk, and a frame that sees one consistent set of numbers.

⚠ A finished task stays in the list until it is pumped away. Otherwise a task that completes between two frames never appears at all, and an import that failed instantly reports nothing — which reads as an import that silently did nothing.

Fields and properties (3)

  • public IReadOnlyList<BackgroundTask> Tasks

    What is running, oldest first.

  • public bool IsBusy

    Whether anything is running.

  • public float Progress

    How far along everything is together, from zero to one.

Events (2)

  • public Action<BackgroundTaskManager>? Changed

    Raised after Pump applies anything, and when a task starts.

  • public Action<BackgroundTask>? Ended

    Raised once per task, after it stops, whatever it stopped as.

Methods (7)

  • public BackgroundTask Begin(string title)

    Starts a task the caller drives itself.

  • public BackgroundTask Start(string title, Func<BackgroundTask, Task> work)

    Starts a task and runs it.

  • public void Complete(BackgroundTask task)

    Says a task the caller was driving has finished.

  • public void Fail(BackgroundTask task, Exception? failure = null)

    Says a task the caller was driving has failed.

  • public void CancelAll()

    Asks everything running to stop.

  • public void Pump(int budget = 4096)

    Applies everything the tasks have reported since the last frame.

  • public void Dispose()

    Asks everything to stop and stops listening to what is still running.

Used by (11)

  • BackgroundTaskVixen.Ui
  • BackgroundTaskTestsVixen.Ui.Tests
  • UiApplicationVixen.Ui.Desktop
  • UiApplicationTestsVixen.Ui.Desktop.Tests
  • ContentTasksVixen.Editor.App
  • EditorShellVixen.Editor.Ui
  • EditorShellTestsVixen.Editor.Ui.Tests
  • EditorStillnessTestsVixen.Editor.App.Tests
  • MeshMapBakeGeometryTestsVixen.Editor.App.Tests
  • TaskCenterVixen.Editor.Ui
  • TaskCenterTestsVixen.Editor.Ui.Tests