{"record":{"id":"f2f5707dc851431e","repo":"dotnet/wpf","slug":"sr-invalidoperationduringclosing","errorCode":null,"errorMessage":"SR.InvalidOperationDuringClosing","messagePattern":"SR\\.InvalidOperationDuringClosing","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":3701,"sourceCode":"            {\n                throw new ObjectDisposedException(null, SR.WindowDisposed);\n            }\n#endif\n        }\n\n        private void VerifyCanShow()\n        {\n            if (_disposed)\n            {\n                throw new InvalidOperationException(SR.ReshowNotAllowed);\n            }\n        }\n\n        private void VerifyNotClosing()\n        {\n            if (_isClosing)\n            {\n                throw new InvalidOperationException(SR.InvalidOperationDuringClosing);\n            }\n\n            if (!IsSourceWindowNull && IsCompositionTargetInvalid)\n            {\n                throw new InvalidOperationException(SR.InvalidCompositionTarget);\n            }\n        }\n\n        private void VerifyHwndCreateShowState()\n        {\n            if (HwndCreatedButNotShown)\n            {\n                throw new InvalidOperationException(SR.NotAllowedBeforeShow);\n            }\n        }\n\n        /// <summary>\n        ///     sets the IWindowService attached property","sourceCodeStart":3683,"sourceCodeEnd":3719,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L3683-L3719","documentation":"VerifyNotClosing throws InvalidOperationException with InvalidOperationDuringClosing when a window operation is attempted while the window is in the middle of closing (_isClosing == true). During the close sequence (including Closing event handling) most operations like Show, Activate, or setting properties are disallowed. It also throws InvalidCompositionTarget right after, but this error is the closing-state guard.","triggerScenarios":"Calling Show()/ShowDialog()/owner changes from inside the Closing event handler or during the WM_CLOSE processing pipeline.","commonSituations":"Trying to reopen or cancel-close by re-showing the window in OnClosing; opening a new window and mutating the closing one; async callbacks landing while close is in flight.","solutions":["Perform follow-up work after the Closed event fires instead of during Closing.","Cancel a close properly via CancelEventArgs.Cancel = true in the Closing handler rather than re-showing.","Defer UI mutations to the Dispatcher after close completes (Dispatcher.BeginInvoke from Closed)."],"exampleFix":"// before\nprivate void Window_Closing(object s, CancelEventArgs e) {\n    otherWindow.Owner = this; // throws: window is closing\n}\n// after\nprivate void Window_Closed(object s, EventArgs e) {\n    // safe to run cleanup / final operations here\n}","handlingStrategy":"validation","validationCode":"bool safeToOperate(Window w) => !w.IsVisible == false && !w.IsLoaded == false; // better: track _isClosing via Closing/Closed events","typeGuard":null,"tryCatchPattern":"try { w.Show(); }\ncatch (InvalidOperationException) { /* defer until Closed fires */ }","preventionTips":["Do work after Closed, not inside Closing.","Cancel closes with CancelEventArgs.Cancel, not by re-showing.","Guard async continuations so they don't touch a window mid-close."],"tags":["wpf","window","closing","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-22T01:17:13.364Z"}