dotnet/wpf · error · InvalidOperationException

SR.InvalidOperationDuringClosing

Error message

SR.InvalidOperationDuringClosing

What it means

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.

Solutions

  1. Perform follow-up work after the Closed event fires instead of during Closing.
  2. Cancel a close properly via CancelEventArgs.Cancel = true in the Closing handler rather than re-showing.
  3. Defer UI mutations to the Dispatcher after close completes (Dispatcher.BeginInvoke from Closed).

Example fix

// before
private void Window_Closing(object s, CancelEventArgs e) {
    otherWindow.Owner = this; // throws: window is closing
}
// after
private void Window_Closed(object s, EventArgs e) {
    // safe to run cleanup / final operations here
}
Defensive patterns

Strategy: validation

Validate before calling

bool safeToOperate(Window w) => !w.IsVisible == false && !w.IsLoaded == false; // better: track _isClosing via Closing/Closed events

Try / catch

try { w.Show(); }
catch (InvalidOperationException) { /* defer until Closed fires */ }

Prevention

When it happens

Trigger: Calling Show()/ShowDialog()/owner changes from inside the Closing event handler or during the WM_CLOSE processing pipeline.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/f2f5707dc851431e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs:3701

            {
                throw new ObjectDisposedException(null, SR.WindowDisposed);
            }
#endif
        }

        private void VerifyCanShow()
        {
            if (_disposed)
            {
                throw new InvalidOperationException(SR.ReshowNotAllowed);
            }
        }

        private void VerifyNotClosing()
        {
            if (_isClosing)
            {
                throw new InvalidOperationException(SR.InvalidOperationDuringClosing);
            }

            if (!IsSourceWindowNull && IsCompositionTargetInvalid)
            {
                throw new InvalidOperationException(SR.InvalidCompositionTarget);
            }
        }

        private void VerifyHwndCreateShowState()
        {
            if (HwndCreatedButNotShown)
            {
                throw new InvalidOperationException(SR.NotAllowedBeforeShow);
            }
        }

        /// <summary>
        ///     sets the IWindowService attached property

View on GitHub (pinned to 81131a70a4)