{"record":{"id":"ff1d6b4e7d69e306","repo":"dotnet/wpf","slug":"sr-applicationalreadyrunning","errorCode":null,"errorMessage":"SR.ApplicationAlreadyRunning","messagePattern":"SR\\.ApplicationAlreadyRunning","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs","lineNumber":2397,"sourceCode":"                            {\n                                string component = (diff == 1) ? bamlConvertUriSegments[1] : curUriSegments[1];\n\n                                isRootElement = BaseUriHelper.IsComponentEntryAssembly(component);\n                            }\n                        }\n                    }\n                }\n            }\n\n            return isRootElement;\n        }\n\n\n        private object RunDispatcher(object ignore)\n        {\n            if (_ownDispatcherStarted)\n            {\n                throw new InvalidOperationException(SR.ApplicationAlreadyRunning);\n            }\n            _ownDispatcherStarted = true;\n            Dispatcher.Run();\n            return null;\n        }\n\n        #endregion Private Methods\n\n        //------------------------------------------------------\n        //\n        //  Private Fields\n        //\n        //------------------------------------------------------\n\n        #region Private Fields\n        private static object                           _globalLock;\n        private static bool                             _isShuttingDown;\n        private static bool                             _appCreatedInThisAppDomain;","sourceCodeStart":2379,"sourceCodeEnd":2415,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs#L2379-L2415","documentation":"InvalidOperationException with resource key SR.ApplicationAlreadyRunning is thrown by Application.RunDispatcher when the application's own dispatcher has already been started. WPF's Application may start its message pump (Dispatcher.Run) only once; a second Run/Run(Window) call on the same Application instance while the pump is active is an invalid state and is rejected.","triggerScenarios":"Calling Application.Run() or Application.Run(Window) a second time on the same Application instance while the first dispatcher loop is still running, or re-entering Run from code executed on the app's UI thread (e.g. an event handler or Dispatcher callback that calls Run again).","commonSituations":"Calling app.Run() twice (e.g. in Main and again in a restart path); spawning a second WPF app inside an event handler; attempting to 'restart' the app by calling Run instead of launching a new process; mixing custom Dispatcher.Run() with Application.Run().","solutions":["Remove the duplicate Application.Run() call; Run should be called exactly once from the entry point.","To restart, call Application.Current.Shutdown(), exit the current process, and launch a new process instead of calling Run again.","Guard the Run call: only invoke it when the dispatcher is not running (Dispatcher.CurrentDispatcher.HasShutdownFinished).","If extra message processing is needed, use Dispatcher.Invoke/BeginInvoke on the existing dispatcher rather than starting a new pump."],"exampleFix":"// before\npublic static void Main()\n{\n    var app = new App();\n    app.Run();\n    app.Run(); // InvalidOperationException: ApplicationAlreadyRunning\n}\n// after\npublic static void Main()\n{\n    var app = new App();\n    app.Run(); // called exactly once\n}","handlingStrategy":"validation","validationCode":"bool canRun = Application.Current == null || Application.Current.Dispatcher == null || Application.Current.Dispatcher.HasShutdownFinished;\nif (!canRun) throw new InvalidOperationException(\"Application dispatcher already running; do not call Run() again.\");","typeGuard":"bool CanCallRun(Application app) => app == null || app.Dispatcher == null || app.Dispatcher.HasShutdownFinished;","tryCatchPattern":"try { app.Run(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already running\"))\n{\n    // dispatcher already active; skip Run\n}","preventionTips":["Call Application.Run exactly once, from Main only.","Restart by launching a new process, not by re-calling Run.","Use Dispatcher.Invoke/BeginInvoke for extra work on the running dispatcher."],"tags":["wpf","application-lifecycle","dispatcher","invalid-operation"],"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-21T21:30:21.729Z"}