{"record":{"id":"9758da994145f00e","repo":"dotnet/wpf","slug":"sr-applicationshuttingdown","errorCode":null,"errorMessage":"SR.ApplicationShuttingDown","messagePattern":"SR\\.ApplicationShuttingDown","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs","lineNumber":2043,"sourceCode":"                part = resContainer.GetPart(partUri);\n            }\n\n            return part;\n        }\n\n        /// <summary> Helper for getting the pack://application or pack://siteoforigin resource package. </summary>\n        /// <param name=\"packageUri\"> \"application://\" or \"siteoforigin://\" </param>\n        private static Package GetResourcePackage(Uri packageUri)\n        {\n            Package package = PreloadedPackages.GetPackage(packageUri);\n            if (package == null)\n            {\n                Uri packUri = PackUriHelper.Create(packageUri);\n                Invariant.Assert(packUri == BaseUriHelper.PackAppBaseUri || packUri == BaseUriHelper.SiteOfOriginBaseUri,\n                    $\"Unknown packageUri passed: {packageUri}\");\n\n                Invariant.Assert(IsApplicationObjectShuttingDown);\n                throw new InvalidOperationException(SR.ApplicationShuttingDown);\n            }\n            return package;\n        }\n\n        /// <summary>\n        ///     Creates hwndsource so that we can listen to some window msgs.\n        /// </summary>\n        private void EnsureHwndSource()\n        {\n            if (_parkingHwnd == null)\n            {\n                // _appFilterHook needs to be member variable otherwise\n                // it is GC'ed and we don't get messages from HwndWrapper\n                // (HwndWrapper keeps a WeakReference to the hook)\n\n                _appFilterHook = new HwndWrapperHook(AppFilterMessage);\n                HwndWrapperHook[] wrapperHooks = {_appFilterHook};\n","sourceCodeStart":2025,"sourceCodeEnd":2061,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs#L2025-L2061","documentation":"GetResourcePackage reached a state where the requested package was not found while the application is shutting down; an Invariant.Assert confirmed IsApplicationObjectShuttingDown, and the method then throws InvalidOperationException(SR.ApplicationShuttingDown). This is effectively an internal invariant path: during shutdown, resource packages are being torn down, so the package is no longer available.","triggerScenarios":"Requesting a resource/package (via pack URI resolution, GetContentStream/GetRemoteStream internals) after Shutdown began; background threads still resolving pack URIs while the Exit event fires; image bindings resolving sources during window close.","commonSituations":"Async image loads racing application shutdown; timer/dispatcher callbacks firing after Shutdown; pack URI access inside Exit handlers; leaks where a background worker outlives the app lifetime.","solutions":["Check Application.Current == null or Dispatcher.HasShutdownStarted before resolving resources on background code","Cancel outstanding async resource loads in OnExit / ShutdownStarted","Load needed resources before initiating Shutdown, and cache streams","Marshal shutdown-sensitive work onto the dispatcher before shutdown starts; guard with try-catch on InvalidOperationException in teardown paths"],"exampleFix":"// before\nvar s = Application.GetContentStream(uri); // may hit shutdown teardown\n// after\nif (Application.Current != null && !Application.Current.Dispatcher.HasShutdownStarted)\n    var s = Application.GetContentStream(uri);","handlingStrategy":"try-catch","validationCode":"bool canLoadResources() => Application.Current != null && !Application.Current.Dispatcher.HasShutdownStarted;","typeGuard":"bool AppAlive(Application app) => app is not null && !app.Dispatcher.HasShutdownStarted;","tryCatchPattern":"try { var s = Application.GetContentStream(uri); } catch (InvalidOperationException) { /* app is shutting down; abort resource load */ }","preventionTips":["Cancel async loads on ShutdownStarted","Cache resources before shutdown","Guard background threads with HasShutdownStarted checks","Avoid pack URI resolution inside Exit handlers"],"tags":["wpf","shutdown","race-condition","resources","lifecycle"],"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"}