{"record":{"id":"4210ee82ba498edd","repo":"tui-cs/Terminal.Gui","slug":"cannot-use-modern-instance-based-model-applicatio-4210ee","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.cs","lineNumber":143,"sourceCode":"                // Mark the usage and create the instance\n                ModelUsage = ApplicationModelUsage.LegacyStatic;\n\n                return _instance = new ApplicationImpl ();\n            }\n        }\n    }\n\n    /// <summary>\n    ///     INTERNAL: Marks that the instance-based model has been used. Called by Application.Create().\n    /// </summary>\n    internal static void MarkInstanceBasedModelUsed ()\n    {\n        lock (_modelUsageLock)\n        {\n            // Check if the legacy static model has already been initialized\n            if (ModelUsage == ApplicationModelUsage.LegacyStatic && _instance?.Initialized == true)\n            {\n                throw new InvalidOperationException (ERROR_MODERN_AFTER_LEGACY);\n            }\n\n            ModelUsage = ApplicationModelUsage.InstanceBased;\n        }\n    }\n\n    /// <summary>\n    ///     INTERNAL: Resets the model usage tracking. Only for testing purposes.\n    /// </summary>\n    internal static void ResetModelUsageTracking ()\n    {\n        lock (_modelUsageLock)\n        {\n            ModelUsage = ApplicationModelUsage.None;\n            _instance = null;\n        }\n    }\n","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/App/ApplicationImpl.cs#L125-L161","documentation":"Thrown by ApplicationImpl.MarkInstanceBasedModelUsed (called by Application.Create) when ModelUsage is already LegacyStatic and the legacy _instance is Initialized. This prevents switching to the modern model after the legacy static singleton has been brought up. Error string is ERROR_MODERN_AFTER_LEGACY. It is the mirror of error [4]/[10] at the Create() entry point.","triggerScenarios":"Calling Application.Create() after Application.Init() (legacy) already initialized the singleton; any code path that reaches MarkInstanceBasedModelUsed after the legacy model is live.","commonSituations":"Migrating mid-session from legacy to modern model; a startup sequence that calls Application.Init then later tries Application.Create; test code mixing both models without resetting the fence.","solutions":["Use only one model per process; if legacy static was used, stay with it.","Reset the fence in tests with ApplicationImpl.ResetModelUsageTracking() (test-only) before switching models.","Ensure Application.Shutdown()/Dispose fully tears down the legacy instance before attempting Create (though mixing is still disallowed in one process).","Run the conflicting model in a separate process."],"exampleFix":"// before\nApplication.Init ();\nusing var app = Application.Create (); // MarkInstanceBasedModelUsed throws\n\n// after — choose one model\nusing var app = Application.Create ().Init ();","handlingStrategy":"validation","validationCode":"// Call Application.Create() before any Application.Init() in the process.\n// Do not switch models mid-process.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide on one model at startup and stick with it.","Reset the fence only in tests via ApplicationImpl.ResetModelUsageTracking().","Never call Application.Create after Application.Init in the same process."],"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"}