{"record":{"id":"5327587f62946d1a","repo":"tui-cs/Terminal.Gui","slug":"cannot-use-legacy-static-application-model-applic-532758","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.cs","lineNumber":122,"sourceCode":"    {\n        get\n        {\n            //Debug.Fail (\"ApplicationImpl.Instance accessed - parallelizable tests should not use legacy static Application model\");\n\n            // Thread-safe: Use lock to make check-and-create atomic\n            lock (_modelUsageLock)\n            {\n                // If an instance already exists, return it without fence checking\n                // This allows for cleanup/reset operations\n                if (_instance is { })\n                {\n                    return _instance;\n                }\n\n                // Check if the instance-based model has already been used\n                if (ModelUsage == ApplicationModelUsage.InstanceBased)\n                {\n                    throw new InvalidOperationException (ERROR_LEGACY_AFTER_MODERN);\n                }\n\n                // 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","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/App/ApplicationImpl.cs#L104-L140","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Migrate all code to the instance-based model; never touch the static Application facade after Create().","If a dependency requires the static model, run your host app with the legacy static model throughout.","Isolate conflicting code into a subprocess.","Audit for any 'Application.' static member access and replace with the IApplication instance."],"exampleFix":"// before\nusing var app = Application.Create ().Init ();\nApplication.Top.X = 0; // touches legacy static Instance — throws\n\n// after\nusing var app = Application.Create ().Init ();\n// use the instance API instead of static Application.* members","handlingStrategy":"validation","validationCode":"// After Application.Create(), never touch static Application.* members.\n// Pass the IApplication instance to any code that needs it.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid all static Application.* member access in modern-model apps.","Audit dependencies for legacy static usage before integrating.","Keep one model per 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"}