{"record":{"id":"2921eb175775246c","repo":"dotnet/wpf","slug":"sr-notallowedbeforeshow","errorCode":null,"errorMessage":"SR.NotAllowedBeforeShow","messagePattern":"SR\\.NotAllowedBeforeShow","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":3714,"sourceCode":"\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            {\n                SetValue(IWindowServiceProperty, (IWindowService)this);\n            }\n        }\n\n        private IntPtr GetCurrentMonitorFromMousePosition()\n        {\n            // center on the screen on which the mouse is on\n            NativeMethods.POINT pt = default;","sourceCodeStart":3696,"sourceCodeEnd":3732,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L3696-L3732","documentation":"Window.VerifyHwndCreateShowState throws InvalidOperationException(SR.NotAllowedBeforeShow) when HwndCreatedButNotShown is true — i.e. the window's Win32 HWND has been created but the window has never actually been shown yet. WPF throws because the invoked operation is only meaningful on a window that has been displayed (it has a real, shown native window and position/state).","triggerScenarios":"Calling public Window members that require a shown window (e.g. WindowStartupLocation/state transitions, certain Restore/Focus operations validated by this helper) while the HWND was created but Show()/ShowDialog() has not completed.","commonSituations":"Interacting with a window between construction and Show(); creating a window and immediately querying/mutating window state before the show call; Show() failing silently so the window stays in created-but-not-shown state.","solutions":["Call window.Show() or window.ShowDialog() before invoking operations that require a shown window.","Move state queries/changes into the window's Loaded or ContentRendered event, which fire after the window is shown.","If using Hide()/re-show patterns, verify IsLoaded is true before manipulating the window."],"exampleFix":"// before\nvar w = new MyWindow();\nw.Restore();\n\n// after\nvar w = new MyWindow();\nw.Show();\nw.Restore();","handlingStrategy":"validation","validationCode":"if (!window.IsLoaded) { window.Show(); }","typeGuard":"bool IsShown(Window w) => w != null && w.IsLoaded && w.IsVisible;","tryCatchPattern":"try { w.Restore(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"before\") || ex.Message.Contains(\"NotAllowedBeforeShow\"))\n{ w.Show(); w.Restore(); }","preventionTips":["Defer window-state operations to the Loaded or ContentRendered event.","Never call state-dependent members between constructing the Window and Show()/ShowDialog().","Verify IsVisible/IsLoaded before programmatic window manipulation."],"tags":["wpf","window","invalid-operation","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"}