Gondwana Game Engine
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop, mobile, and web platforms using SkiaSharp for graphics and NAudio for sound.
Loading...
Searching...
No Matches
Gondwana.UiDispatcher Class Referencesealed

Provides a concrete implementation of IUiDispatcher for dispatching actions to the UI thread using a SynchronizationContext. This class enables thread-safe marshalling of operations from background threads to the application's main UI thread. More...

Inheritance diagram for Gondwana.UiDispatcher:
Gondwana.IUiDispatcher

Public Member Functions

 UiDispatcher (SynchronizationContext uiContext)
 Initializes a new instance of the UiDispatcher class with the specified synchronization context.
void Post (Action action)
 Asynchronously posts an action to be executed on the UI thread. This method queues the action and returns immediately without waiting for execution to complete.
void Send (Action action)
 Synchronously sends an action to be executed on the UI thread. If already on the UI thread, the action is executed inline; otherwise, the calling thread blocks until the action completes on the UI thread.

Properties

bool IsOnUIThread [get]
 Gets a value indicating whether the current thread is the UI thread to which this dispatcher is bound.

Detailed Description

Provides a concrete implementation of IUiDispatcher for dispatching actions to the UI thread using a SynchronizationContext. This class enables thread-safe marshalling of operations from background threads to the application's main UI thread.

The UiDispatcher wraps a SynchronizationContext and records the thread ID at construction time to enable fast thread checking. It provides both asynchronous (Post) and synchronous (Send) dispatch methods.

This implementation is thread-safe and can be used from any thread to marshal UI operations back to the thread that created the dispatcher.

Constructor & Destructor Documentation

◆ UiDispatcher()

Gondwana.UiDispatcher.UiDispatcher ( SynchronizationContext uiContext)

Initializes a new instance of the UiDispatcher class with the specified synchronization context.

Parameters
uiContextThe SynchronizationContext associated with the UI thread. This context is used to marshal operations to the UI thread. Must not be null.
Exceptions
ArgumentNullExceptionThrown if uiContext is null.

The constructor captures the current thread's managed thread ID, establishing it as the UI thread for subsequent IsOnUIThread checks. This dispatcher should be constructed on the UI thread to ensure correct thread identification.

Member Function Documentation

◆ Post()

void Gondwana.UiDispatcher.Post ( Action action)

Asynchronously posts an action to be executed on the UI thread. This method queues the action and returns immediately without waiting for execution to complete.

Parameters
actionThe action to execute on the UI thread. The action will be queued to the UI thread's SynchronizationContext and executed asynchronously.

This method uses SynchronizationContext.Post to queue the action, which is non-blocking and suitable for fire-and-forget UI updates from background threads.

The method does not check if the current thread is the UI thread; it always posts the action through the synchronization context. This ensures consistent asynchronous behavior regardless of the calling thread.

This method is thread-safe and can be called from any thread.

Implements Gondwana.IUiDispatcher.

◆ Send()

void Gondwana.UiDispatcher.Send ( Action action)

Synchronously sends an action to be executed on the UI thread. If already on the UI thread, the action is executed inline; otherwise, the calling thread blocks until the action completes on the UI thread.

Parameters
actionThe action to execute on the UI thread. If not already on the UI thread, the calling thread will block until this action completes execution.

This method provides an optimization for calls originating from the UI thread by executing the action inline without marshalling overhead. For calls from other threads, it uses SynchronizationContext.Send to synchronously dispatch the action, blocking the caller until completion.

Warning: Synchronous dispatch can lead to deadlocks if the UI thread is waiting for the calling thread or if there are circular dependencies. Use Post for asynchronous, non-blocking dispatch whenever possible.

This method is thread-safe and can be called from any thread.

Implements Gondwana.IUiDispatcher.

Property Documentation

◆ IsOnUIThread

bool Gondwana.UiDispatcher.IsOnUIThread
get

Gets a value indicating whether the current thread is the UI thread to which this dispatcher is bound.

true if the current thread's managed thread ID matches the UI thread ID captured during construction; otherwise, false.

This property provides a fast thread check by comparing managed thread IDs without requiring synchronization context interaction. It can be used to optimize action execution by avoiding marshalling overhead when already on the UI thread.

Implements Gondwana.IUiDispatcher.