tui-cs/Terminal.Gui · error · NotInitializedException

Invoke

Error message

Invoke

What it means

Thrown as NotInitializedException(nameof(Invoke)) by the Invoke(Action<IApplication>?) overload when Initialized is false. Invoke is meant to marshal work onto the UI thread via the main loop's TimedEvents, which do not exist before Init. The exception message is simply 'Invoke' because it is constructed from the member name.

Source

Thrown at Terminal.Gui/App/ApplicationImpl.Run.cs:60

    #endregion Main Loop Iteration

    #region Timeouts and Invoke

    /// <inheritdoc/>
    public ITimedEvents TimedEvents { get; }

    /// <inheritdoc/>
    public object AddTimeout (TimeSpan time, Func<bool> callback) => TimedEvents.Add (time, callback);

    /// <inheritdoc/>
    public bool RemoveTimeout (object token) => TimedEvents.Remove (token);

    /// <inheritdoc/>
    public void Invoke (Action<IApplication>? action)
    {
        if (!Initialized)
        {
            throw new NotInitializedException (nameof (Invoke));
        }

        // If we are already on the main UI thread
        if (TopRunnableView is IRunnable { IsRunning: true } && MainThreadId == Thread.CurrentThread.ManagedThreadId)
        {
            action?.Invoke (this);

            return;
        }

        TimedEvents.Add (TimeSpan.Zero,
                         () =>
                         {
                             action?.Invoke (this);

                             return false;
                         });
    }

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Ensure app.Init() has run before any Invoke call: guard with 'if (app.Initialized) app.Invoke(...);' or defer the call until after Init.
  2. Start background work from inside the Run loop or after the InitializedChanged event fires.
  3. In tests, use the provided test harness that initializes the application before invoking.

Example fix

// before
app.Invoke (a => UpdateStatus ());

// after
if (app.Initialized)
{
    app.Invoke (a => UpdateStatus ());
}
Defensive patterns

Strategy: validation

Validate before calling

if (app.Initialized)
{
    app.Invoke (a => DoWork ());
}

Type guard

static bool CanInvoke (IApplication app) => app.Initialized;

Prevention

When it happens

Trigger: Calling app.Invoke(action) before app.Init() has completed; calling Invoke from a background thread that started before initialization finished; using Invoke in a constructor or static initializer that runs before the app is ready.

Common situations: Background workers spawned at startup that race ahead of Init; unit tests that call Invoke without setting up an initialized application; refactoring that moved an Invoke call earlier in the lifecycle.

Related errors


AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13). Data as JSON: /api/errors/84c02b482b79a0d8. Report an issue: GitHub.