{"record":{"id":"92b649902f07c0a3","repo":"tui-cs/Terminal.Gui","slug":"cannot-use-modern-instance-based-model-applicatio","errorCode":null,"errorMessage":"Cannot use modern instance-based model (Application.Create) after using legacy static Application model (Application.Init/ApplicationImpl.Instance). Use only one model per process.","messagePattern":"Cannot use modern instance-based model \\(Application\\.Create\\) after using legacy static Application model \\(Application\\.Init/ApplicationImpl\\.Instance\\)\\. Use only one model per process\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"Terminal.Gui/App/ApplicationImpl.Lifecycle.cs","lineNumber":54,"sourceCode":"        // In Application.Init(), before starting the input thread:\n        // Pre-warm the Wcwidth static cache to prevent ZeroTable._lookup race condition\n        // See: https://github.com/spectreconsole/wcwidth/issues/11\n        _ = UnicodeCalculator.GetWidth (new Rune ('A'));\n\n        // Thread-safe fence check: Ensure we're not mixing application models\n        // Use lock to make check-and-set atomic\n        lock (_modelUsageLock)\n        {\n            // If this is a legacy static instance and instance-based model was used, throw\n            if (this == _instance && ModelUsage == ApplicationModelUsage.InstanceBased)\n            {\n                throw new InvalidOperationException (ERROR_LEGACY_AFTER_MODERN);\n            }\n\n            // If this is an instance-based instance and legacy static model was used, throw\n            if (this != _instance && ModelUsage == ApplicationModelUsage.LegacyStatic)\n            {\n                throw new InvalidOperationException (ERROR_MODERN_AFTER_LEGACY);\n            }\n\n            // If no model has been set yet, set it now based on which instance this is\n            if (ModelUsage == ApplicationModelUsage.None)\n            {\n                ModelUsage = this == _instance ? ApplicationModelUsage.LegacyStatic : ApplicationModelUsage.InstanceBased;\n            }\n        }\n\n        if (!string.IsNullOrWhiteSpace (driverName))\n        {\n            _driverName = driverName;\n        }\n\n        if (string.IsNullOrWhiteSpace (_driverName))\n        {\n            _driverName = ForceDriver;\n        }","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/App/ApplicationImpl.Lifecycle.cs#L36-L72","documentation":"Thrown inside Init when a non-singleton (instance-based) ApplicationImpl is being initialized but the process already used the legacy static model (the _instance singleton was initialized first). The error string is ERROR_MODERN_AFTER_LEGACY. It is the mirror of the ERROR_LEGACY_AFTER_MODERN check — the two models cannot coexist in one process because they share global driver and main-loop state.","triggerScenarios":"Calling Application.Create().Init() after Application.Init() (legacy static) was already invoked; code that mixes Application.Init with Application.Create within the same process.","commonSituations":"Migrating an app piecemeal from legacy static to instance-based and leaving a stray Application.Init call; a library dependency that calls Application.Init while the host uses Create.","solutions":["Standardize on the modern instance-based model and remove all Application.Init/Run/Shutdown calls.","If you must keep legacy static, do not call Application.Create anywhere in the process.","Run conflicting code in separate processes.","In tests, reset the fence with ApplicationImpl.ResetModelUsageTracking() between incompatible cases."],"exampleFix":"// before\nApplication.Init (); // legacy static first\n// ...\nusing var app = Application.Create ().Init (); // throws\n\n// after — modern model only\nusing var app = Application.Create ().Init ();\napp.Run<MyWindow> ();","handlingStrategy":"validation","validationCode":"// Ensure the legacy static model was never used before calling Create.\n// Best practice: call Application.Create() first in Main, before any legacy API.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call Application.Create() before any legacy static Application.* access.","Remove all legacy Application.Init/Run/Shutdown calls when migrating.","Keep conflicting code in separate processes."],"tags":["lifecycle","api-model","static-state"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}