{"record":{"id":"187cd46740009715","repo":"tui-cs/Terminal.Gui","slug":"cannot-use-legacy-static-application-model-applic","errorCode":null,"errorMessage":"Cannot use legacy static Application model (Application.Init/ApplicationImpl.Instance) after using modern instance-based model (Application.Create). Use only one model per process.","messagePattern":"Cannot use legacy static Application model \\(Application\\.Init/ApplicationImpl\\.Instance\\) after using modern instance-based model \\(Application\\.Create\\)\\. Use only one model per process\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"Terminal.Gui/App/ApplicationImpl.Lifecycle.cs","lineNumber":48,"sourceCode":"        }\n\n        MainThreadId = Thread.CurrentThread.ManagedThreadId;\n\n        Trace.Lifecycle (MainThreadId?.ToString (), \"Init\", $\"driverName: {driverName}\");\n\n        // 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;","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/App/ApplicationImpl.Lifecycle.cs#L30-L66","documentation":"Thrown inside Init when the legacy static Application singleton (_instance) is being initialized but the process already used the modern instance-based model (Application.Create). Terminal.Gui v2 forbids mixing the two models because they share global driver/main-loop state; using both would corrupt the synchronization context and driver ownership. The error string is ERROR_LEGACY_AFTER_MODERN.","triggerScenarios":"Calling Application.Init() (legacy static facade) after Application.Create() was already invoked elsewhere in the same process; a third-party library or test helper that uses the legacy static API while the host app uses the modern instance-based API.","commonSituations":"Integrating a library written against the v1/legacy static API into a v2 app that uses Application.Create(); mixing UICatalog-style instance code with legacy snippets; a dependency that internally calls Application.Init.","solutions":["Pick one model per process and use it consistently: prefer the modern instance-based model (Application.Create).","If a dependency forces the legacy model, run your app with the legacy static API (Application.Init/Run/Shutdown) throughout.","Isolate conflicting code into a subprocess so each process uses only one model.","Call ApplicationImpl.ResetModelUsageTracking() (test-only) only in controlled test scenarios to reset the fence."],"exampleFix":"// before\nusing var app = Application.Create ().Init ();\n// ... later ...\nApplication.Init (); // legacy static after modern — throws\n\n// after — use only the modern model\nusing var app = Application.Create ().Init ();\napp.Run<MyWindow> ();","handlingStrategy":"validation","validationCode":"// Pick one model at process start and never mix.\n// Modern (recommended):\nusing var app = Application.Create ().Init ();\n// Do NOT call Application.Init() anywhere after this.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Standardize the entire process on one application model.","Audit third-party libraries for Application.Init / Application.* static calls before integrating.","Prefer the modern instance-based model (Application.Create) for new code."],"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"}