dotnet/wpf · error · InvalidOperationException

SR.InvalidCompositionTarget

Error message

SR.InvalidCompositionTarget

What it means

Window.VerifyAccess-style checks throw InvalidOperationException(SR.InvalidCompositionTarget) when the window's HWND exists but its composition target (the rendering pipeline link between the WPF visual layer and the OS window) is invalid or disconnected. This library throws it to prevent operations that would silently no-op or corrupt state when the window can no longer render. The check runs in public Window members (e.g. during Close/Show path validation) guarded by IsSourceWindowNull && IsCompositionTargetInvalid.

Solutions

  1. Check IsCompositionTargetInvalid / IsLoaded before calling the Window API, and skip the call if the target is invalid.
  2. Ensure all window manipulation happens on the UI Dispatcher thread (Dispatcher.Invoke/BeginInvoke from worker threads).
  3. Unhook handlers and cancel pending work in the Closing/Closed events so code does not operate on a dying window.
  4. If the error is intermittent during shutdown, guard shutdown-path logic with Application.Current.CheckAccess() and Dispatcher.HasShutdownFinished checks.

Example fix

// before
window.Activate();

// after
if (window.IsLoaded && !window.IsCompositionTargetInvalid)
{
    window.Activate();
}
Defensive patterns

Strategy: validation

Validate before calling

bool safe = window.IsLoaded && !window.IsCompositionTargetInvalid && !window.IsSourceWindowNull;

Type guard

bool CanOperateOn(Window w) => w != null && w.IsLoaded && !w.IsCompositionTargetInvalid;

Try / catch

try { window.Close(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("composition target") || ex.Message.Contains("InvalidCompositionTarget"))
{ /* window already tearing down; ignore */ }

Prevention

When it happens

Trigger: Calling a public Window method that verifies window state while the window's composition target has been invalidated — typically after the window is closing/has been closed, its HWND is being destroyed, or the rendering target was disconnected, while IsSourceWindowNull is false (the source window reference still exists).

Common situations: Calling Close(), Activate(), or similar members from a background thread or from inside the Closing/ Closed event; racing a Close() with another UI operation; window being torn down during app shutdown while code still touches it.

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/c085c026a202c6c5. Report an issue: GitHub.

Appendix: source

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

        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
        /// </summary>
        private void SetIWindowService()
        {
            if (GetValue(IWindowServiceProperty) == null)
            {

View on GitHub (pinned to 81131a70a4)