{"record":{"id":"0afd20ab4dfd17b8","repo":"dotnet/wpf","slug":"sr-format-sr-cannotcallrunmultipletimes-this-gettype","errorCode":null,"errorMessage":"SR.Format(SR.CannotCallRunMultipleTimes, this.GetType().FullName)","messagePattern":"SR\\.Format\\(SR\\.CannotCallRunMultipleTimes, this\\.GetType\\(\\)\\.FullName\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs","lineNumber":1680,"sourceCode":"\n            EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordGeneral | EventTrace.Keyword.KeywordPerf, EventTrace.Event.WClientAppRun);\n\n            //\n            // (Can't create app and do run/shutdown followed\n            // by run/shutdown)\n            //\n            // Devs could write the following code\n            //\n            // Application app = new Application();\n            // app.Run();\n            // app.Run();\n            //\n            // In this case, we should throw an exception when Run is called for the second time.\n            // When app is shutdown, _appIsShutdown is set to true.  If it is true here, then we\n            // throw an exception\n            if (_appIsShutdown)\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotCallRunMultipleTimes, this.GetType().FullName));\n            }\n\n            if (window != null)\n            {\n                if (!window.CheckAccess())\n                {\n                    throw new ArgumentException(SR.Format(SR.WindowPassedShouldBeOnApplicationThread, window.GetType().FullName, this.GetType().FullName));\n                }\n\n                if (!WindowsInternal.HasItem(window))\n                {\n                    WindowsInternal.Add(window);\n                }\n\n                if (MainWindow == null)\n                {\n                    MainWindow = window;\n                }","sourceCodeStart":1662,"sourceCodeEnd":1698,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs#L1662-L1698","documentation":"Application.Run can only execute the application's message loop once. When Run (or Run(Window)) is called after the application has already shut down (_appIsShutdown true), WPF throws InvalidOperationException naming the application type. A single Application instance manages exactly one lifetime.","triggerScenarios":"Calling app.Run() a second time after Shutdown() completed; restarting the loop from Exit handlers; re-invoking Run after Run(Window) returned because the window closed; test code reusing an Application instance across test cases.","commonSituations":"Attempts to 'restart' an app without creating a new Application; repeated Run calls in loops; unit-test fixtures instantiating one Application but calling Run per test.","solutions":["Call Run exactly once per Application instance, at the entry point (Main/App.g.cs)","To restart, create a new Application instance (in a fresh process if needed) and Run that","Check Application.Current == null or _appIsShutdown semantics — track whether Run already returned before calling again","In tests, build a fresh Application (or avoid Run entirely) per fixture"],"exampleFix":"// before\napp.Run();\n// ... later, same instance\napp.Run(); // throws\n// after\napp.Run();\n// restart with a new instance\nvar app2 = new MyApp();\napp2.Run();","handlingStrategy":"type-guard","validationCode":"bool canRun() => Application.Current == null || !Application.Current.Dispatcher.HasShutdownStarted;","typeGuard":"bool RunAlreadyCompleted(MyApp app) => app.Dispatcher.HasShutdownFinished;","tryCatchPattern":"try { app.Run(); } catch (InvalidOperationException) { /* create a new Application instance to restart */ }","preventionTips":["Call Run once from the app entry point","Create a new Application to restart","Never call Run from event handlers","In tests, instantiate a fresh Application per case"],"tags":["wpf","lifecycle","message-loop","run","invalid-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}