logo SERENITY
Game Systems Foundation
Search Results for

    Show / Hide Table of Contents

    Class UnityMainThreadDispatcher

    Unity-specific main-thread dispatcher. Captures the UnitySynchronizationContext and dispatches calls to it when the target type or method parameters involve Unity API types.

    Inheritance
    object
    UnityMainThreadDispatcher
    Implements
    IMainThreadDispatcher
    Inherited Members
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    object.GetType()
    Namespace: Serenity.UnityGlobal.Infrastructure.Services
    Assembly: Serenity.UnityGlobal.Infrastructure.dll
    Syntax
    public sealed class UnityMainThreadDispatcher : IMainThreadDispatcher

    Constructors

    UnityMainThreadDispatcher()

    Declaration
    public UnityMainThreadDispatcher()

    Properties

    IsAvailable

    Declaration
    public bool IsAvailable { get; }
    Property Value
    Type Description
    bool

    Methods

    CaptureContext()

    Captures the current synchronization context if it is the Unity context. Call this once from the main thread (e.g. during bootstrap).

    Declaration
    public void CaptureContext()

    InvokeOnMainThreadAsync(Func<Task>)

    Declaration
    public Task InvokeOnMainThreadAsync(Func<Task> action)
    Parameters
    Type Name Description
    Func<Task> action
    Returns
    Type Description
    Task
    Remarks

    WebGL continuation note (Slice Y): the two await … .ConfigureAwait(true) calls below are deliberate. Continuations from these awaits MUST be posted via the captured Unity SynchronizationContext (which Unity drains every Update) rather than left to the default TaskScheduler. On WebGL there is no real thread pool — when a long async chain (e.g. a cutscene state machine running hundreds of Yield() cycles) completes, the awaited Task transitions to RanToCompletion via MoveNext machinery on the main thread, but ConfigureAwait(false) continuations end up scheduled on the default scheduler which WebGL never effectively pumps — so the continuation queues and never runs, stalling the entire init pipeline silently after the cutscene's last log line. ConfigureAwait(true) forces continuation through Post(SendOrPostCallback, object), guaranteeing it runs on the next frame.

    RequiresMainThread(MethodInfo, Type)

    Declaration
    public bool RequiresMainThread(MethodInfo method, Type targetType)
    Parameters
    Type Name Description
    MethodInfo method
    Type targetType
    Returns
    Type Description
    bool

    Implements

    IMainThreadDispatcher
    In this article
    © 2026 Serenity. All Rights Reserved