{"record":{"id":"45b269f16855e6b6","repo":"dotnet/wpf","slug":"sr-showdialogonvisible","errorCode":null,"errorMessage":"SR.ShowDialogOnVisible","messagePattern":"SR\\.ShowDialogOnVisible","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":280,"sourceCode":"        ///     Shows the window as a modal window\n        /// </summary>\n        /// <returns>bool?</returns>\n        /// <remarks>\n        ///     Callers must have UIPermission(UIPermissionWindow.AllWindows) to call this API.\n        /// </remarks>\n        public Nullable<bool> ShowDialog()\n        {\n            // this call ends up throwing an exception if ShowDialog\n            // is not allowed\n            VerifyApiSupported();\n            VerifyContextAndObjectState();\n            VerifyCanShow();\n            VerifyNotClosing();\n            VerifyConsistencyWithAllowsTransparency();\n\n            if (_isVisible)\n            {\n                throw new InvalidOperationException(SR.ShowDialogOnVisible);\n            }\n            else if (_showingAsDialog)\n            {\n                throw new InvalidOperationException(SR.ShowDialogOnModal);\n            }\n\n            _dialogOwnerHandle = _ownerHandle;\n\n            // verify owner handle is window\n            if (!UnsafeNativeMethods.IsWindow( new HandleRef( null, _dialogOwnerHandle ) ))\n            {\n                _dialogOwnerHandle = IntPtr.Zero;\n            }\n\n\n            // remember the current active window;\n            // this is used when dialog creation fails or dialog closes, we set the active window back to this one.\n            _dialogPreviousActiveHandle = UnsafeNativeMethods.GetActiveWindow();","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L262-L298","documentation":"Window.ShowDialog shows a window modally and requires the window not already be visible. If ShowDialog is called while the window is already shown (_isVisible), WPF throws InvalidOperationException(SR.ShowDialogOnVisible). A window cannot become modal twice.","triggerScenarios":"Calling ShowDialog() on a Window instance whose Show() or ShowDialog() has already been called and which has not closed; re-invoking ShowDialog from a button handler after the dialog is already displayed.","commonSituations":"Double-click on a UI button firing ShowDialog twice; calling ShowDialog in a loop or re-entrantly from dialog events; reusing a cached window instance that is still open instead of creating a new one.","solutions":["Check window.IsVisible before calling ShowDialog and skip/queue the call if already visible","Create a new Window instance for each modal session instead of reusing one","Disable the invoking button while the dialog is open","Track dialog state and return the existing DialogResult rather than re-showing"],"exampleFix":"// before\nprivate void OpenSettings() {\n    _settingsWindow.ShowDialog(); // throws if already visible\n}\n// after\nprivate void OpenSettings() {\n    if (_settingsWindow == null || !_settingsWindow.IsVisible) {\n        _settingsWindow = new SettingsWindow();\n        _settingsWindow.ShowDialog();\n    }\n}","handlingStrategy":"validation","validationCode":"if (!window.IsVisible) { window.ShowDialog(); }","typeGuard":"bool CanShowDialog(Window w) => !w.IsVisible && !w.IsLoaded || !w.IsVisible;","tryCatchPattern":"try { window.ShowDialog(); } catch (InvalidOperationException ex) when (ex.Message == SR.ShowDialogOnVisible) { window.Activate(); }","preventionTips":["Create a new Window instance per modal session","Check IsVisible before ShowDialog","Disable trigger buttons while the dialog is open"],"tags":["wpf","window","showdialog","modal"],"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"}