tui-cs/Terminal.Gui · critical · InvalidOperationException

Cannot use legacy static Application model (Application.Init

Error message

Cannot use legacy static Application model (Application.Init/ApplicationImpl.Instance) after using modern instance-based model (Application.Create). Use only one model per process.

What it means

Thrown by the ApplicationImpl.Instance getter (legacy static singleton access) when ModelUsage indicates the instance-based model was already used (Application.Create). This is the same fence as error [3] but triggered at the point of accessing the static Instance property rather than inside Init. Error string is ERROR_LEGACY_AFTER_MODERN. Any code touching the legacy static facade after Application.Create() trips this.

Source

Thrown at Terminal.Gui/App/ApplicationImpl.cs:122

    {
        get
        {
            //Debug.Fail ("ApplicationImpl.Instance accessed - parallelizable tests should not use legacy static Application model");

            // Thread-safe: Use lock to make check-and-create atomic
            lock (_modelUsageLock)
            {
                // If an instance already exists, return it without fence checking
                // This allows for cleanup/reset operations
                if (_instance is { })
                {
                    return _instance;
                }

                // Check if the instance-based model has already been used
                if (ModelUsage == ApplicationModelUsage.InstanceBased)
                {
                    throw new InvalidOperationException (ERROR_LEGACY_AFTER_MODERN);
                }

                // Mark the usage and create the instance
                ModelUsage = ApplicationModelUsage.LegacyStatic;

                return _instance = new ApplicationImpl ();
            }
        }
    }

    /// <summary>
    ///     INTERNAL: Marks that the instance-based model has been used. Called by Application.Create().
    /// </summary>
    internal static void MarkInstanceBasedModelUsed ()
    {
        lock (_modelUsageLock)
        {
            // Check if the legacy static model has already been initialized

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Migrate all code to the instance-based model; never touch the static Application facade after Create().
  2. If a dependency requires the static model, run your host app with the legacy static model throughout.
  3. Isolate conflicting code into a subprocess.
  4. Audit for any 'Application.' static member access and replace with the IApplication instance.

Example fix

// before
using var app = Application.Create ().Init ();
Application.Top.X = 0; // touches legacy static Instance — throws

// after
using var app = Application.Create ().Init ();
// use the instance API instead of static Application.* members
Defensive patterns

Strategy: validation

Validate before calling

// After Application.Create(), never touch static Application.* members.
// Pass the IApplication instance to any code that needs it.

Prevention

When it happens

Trigger: Touching any Application.* static property/method that routes through ApplicationImpl.Instance after Application.Create() was called; a legacy dependency calling Application.Init or Application.Top after the host used the modern model.

Common situations: Integrating a v1-era library into a v2 modern-model app; static cleanup code that touches Application.Shutdown/ResetState via the legacy facade; code that reads Application.Top or Application.Driver statically.

Related errors


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