{"record":{"id":"c085c026a202c6c5","repo":"dotnet/wpf","slug":"sr-invalidcompositiontarget","errorCode":null,"errorMessage":"SR.InvalidCompositionTarget","messagePattern":"SR\\.InvalidCompositionTarget","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":3706,"sourceCode":"\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\n        /// </summary>\n        private void SetIWindowService()\n        {\n            if (GetValue(IWindowServiceProperty) == null)\n            {","sourceCodeStart":3688,"sourceCodeEnd":3724,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L3688-L3724","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Check IsCompositionTargetInvalid / IsLoaded before calling the Window API, and skip the call if the target is invalid.","Ensure all window manipulation happens on the UI Dispatcher thread (Dispatcher.Invoke/BeginInvoke from worker threads).","Unhook handlers and cancel pending work in the Closing/Closed events so code does not operate on a dying window.","If the error is intermittent during shutdown, guard shutdown-path logic with Application.Current.CheckAccess() and Dispatcher.HasShutdownFinished checks."],"exampleFix":"// before\nwindow.Activate();\n\n// after\nif (window.IsLoaded && !window.IsCompositionTargetInvalid)\n{\n    window.Activate();\n}","handlingStrategy":"validation","validationCode":"bool safe = window.IsLoaded && !window.IsCompositionTargetInvalid && !window.IsSourceWindowNull;","typeGuard":"bool CanOperateOn(Window w) => w != null && w.IsLoaded && !w.IsCompositionTargetInvalid;","tryCatchPattern":"try { window.Close(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"composition target\") || ex.Message.Contains(\"InvalidCompositionTarget\"))\n{ /* window already tearing down; ignore */ }","preventionTips":["Only touch Window objects on the UI dispatcher thread.","Unhook Closing/Closed handlers early and stop async work when the window closes.","Gate shutdown-path window calls behind Application.Current.Dispatcher.HasShutdownFinished checks."],"tags":["wpf","window","invalid-operation","rendering"],"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"}