{"record":{"id":"b92e37b93d6f9bc8","repo":"dotnet/wpf","slug":"sr-showdialogonmodal","errorCode":null,"errorMessage":"SR.ShowDialogOnModal","messagePattern":"SR\\.ShowDialogOnModal","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":284,"sourceCode":"        ///     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();\n\n            // if owner window is not specified, we get the current active window on this thread's\n            // message queue as the owner.\n            if (_dialogOwnerHandle == IntPtr.Zero)","sourceCodeStart":266,"sourceCodeEnd":302,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L266-L302","documentation":"Only one modal dialog session can run on a Window at a time. If ShowDialog is invoked while the window is already showing as a dialog (_showingAsDialog is true), WPF throws InvalidOperationException(SR.ShowDialogOnModal). This prevents nested/re-entrant modal loops on the same window instance.","triggerScenarios":"Calling ShowDialog() again from within the dialog's own event handlers, from code re-entered during the modal message loop, or from multiple threads/commands targeting the same window instance concurrently.","commonSituations":"A dialog's button handler calling ShowDialog on itself; a command executing ShowDialog re-entrantly while a modal session is active; a shared singleton dialog window opened from two places.","solutions":["Never call ShowDialog on the same window instance while it is modal; show a different (child) window instead","Guard with a _showingAsDialog-equivalent flag or check window.IsVisible before calling","Create a fresh Window instance for each modal invocation","Route repeated requests to the existing dialog and bring it to the foreground instead"],"exampleFix":"// before\nprivate void Reopen(object s, RoutedEventArgs e) {\n    this.ShowDialog(); // re-entrant on same window -> throws\n}\n// after\nprivate void Reopen(object s, RoutedEventArgs e) {\n    if (!_showingAsDialog && !IsVisible) {\n        ShowDialog();\n    } else {\n        Activate(); // bring existing dialog forward\n    }\n}","handlingStrategy":"validation","validationCode":"if (!window.IsVisible && !ReferenceEqualityComparer.Instance.Equals(window, activeWindowShowingDialog)) { window.ShowDialog(); }","typeGuard":"bool CanShowModal(Window w) => !w.IsVisible;","tryCatchPattern":"try { window.ShowDialog(); } catch (InvalidOperationException ex) when (ex.Message == SR.ShowDialogOnModal) { window.Activate(); /* already modal */ }","preventionTips":["Never call ShowDialog from inside the dialog's own handlers","Use a fresh Window instance for nested modals","Serialize ShowDialog calls through one entry point"],"tags":["wpf","window","showdialog","reentrancy"],"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"}