Table of Contents

DispatchHelper

Helper class for dispatching operations to the UI thread in MAUI applications.

Overview

DispatchHelper provides utilities for safely executing code on the main/UI thread, essential for updating UI elements from background threads.

Key Features

  • Thread-Safe UI Updates - Ensure UI operations run on the correct thread
  • Async Support - Both synchronous and asynchronous dispatch methods
  • Dispatcher Access - Easy access to the current dispatcher
  • Queue Management - Efficient queuing of UI operations

Common Use Cases

Dispatch to UI Thread

using GamaLearn;

// Dispatch synchronous action
DispatchHelper.Dispatch(() =>
{
    Label.Text = "Updated from background thread";
});

// Dispatch asynchronous action
await DispatchHelper.DispatchAsync(async () =>
{
    await UpdateUIAsync();
});

Check Current Thread

if (DispatchHelper.IsMainThread)
{
    // Already on UI thread
    UpdateUI();
}
else
{
    // Dispatch to UI thread
    DispatchHelper.Dispatch(() => UpdateUI());
}

See Also