{"record":{"id":"ea628c28f99a64fd","repo":"dotnet/wpf","slug":"sr-shutdownmodewhenappshutdown","errorCode":null,"errorMessage":"SR.ShutdownModeWhenAppShutdown","messagePattern":"SR\\.ShutdownModeWhenAppShutdown","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs","lineNumber":840,"sourceCode":"        /// </summary>\n        public ShutdownMode ShutdownMode\n        {\n            get\n            {\n                VerifyAccess();\n                return _shutdownMode;\n            }\n\n            set\n            {\n                VerifyAccess();\n                if ( !IsValidShutdownMode(value) )\n                {\n                    throw new InvalidEnumArgumentException(\"value\", (int)value, typeof(ShutdownMode));\n                }\n                if (IsShuttingDown || _appIsShutdown)\n                {\n                    throw new InvalidOperationException(SR.ShutdownModeWhenAppShutdown);\n                }\n\n                _shutdownMode = value;\n            }\n        }\n\n        /// <summary>\n        ///     Current locally defined Resources\n        /// </summary>\n        [Ambient]\n        public ResourceDictionary Resources\n        {\n            //Don't use  VerifyAccess() here since Resources can be set from any thread.\n            //We synchronize access using _globalLock\n\n            get\n            {\n                ResourceDictionary resources;","sourceCodeStart":822,"sourceCodeEnd":858,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs#L822-L858","documentation":"ShutdownMode cannot be changed once the application has begun shutting down or is fully shut down. The setter throws InvalidOperationException(SR.ShutdownModeWhenAppShutdown) when IsShuttingDown or _appIsShutdown is true. This protects the shutdown sequence from contradictory configuration mid-flight.","triggerScenarios":"Assigning Application.ShutdownMode from within the Exit event handler, after Shutdown() has been called, inside a Window.Closed handler during app teardown, or on a restarted-but-disposed Application instance.","commonSituations":"Cleanup code in OnExit trying to force OnExplicitShutdown; re-running logic after Application.Run returned; event handlers fired during shutdown cascades.","solutions":["Set ShutdownMode during application startup, before Run() is invoked","Guard the assignment with Application.Current.Dispatcher.HasShutdownStarted checks","Move the setting out of Exit/Closed handlers into App constructor or OnStartup","If the app already shut down, create a new Application instance instead of reconfiguring the old one"],"exampleFix":"// before\nprotected override void OnExit(ExitEventArgs e)\n{\n    Current.ShutdownMode = ShutdownMode.OnExplicitShutdown; // throws\n}\n// after\npublic App()\n{\n    ShutdownMode = ShutdownMode.OnExplicitShutdown;\n}","handlingStrategy":"type-guard","validationCode":"bool canSetMode() => Application.Current != null && !Application.Current.Dispatcher.HasShutdownStarted;","typeGuard":"bool ShutdownPending(Application app) => app.Dispatcher.HasShutdownStarted;","tryCatchPattern":"try { app.ShutdownMode = mode; } catch (InvalidOperationException) { /* app is shutting down; skip */ }","preventionTips":["Set ShutdownMode in the App constructor or OnStartup","Never set it in Exit/Closed handlers","Check HasShutdownStarted before configuring","Keep shutdown configuration at startup code paths only"],"tags":["wpf","shutdown","invalid-state","lifecycle","application"],"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"}