dotnet/wpf · error · InvalidOperationException
SR.ShowDialogOnVisible
Error message
SR.ShowDialogOnVisible
What it means
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.
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
Example fix
// before
private void OpenSettings() {
_settingsWindow.ShowDialog(); // throws if already visible
}
// after
private void OpenSettings() {
if (_settingsWindow == null || !_settingsWindow.IsVisible) {
_settingsWindow = new SettingsWindow();
_settingsWindow.ShowDialog();
}
} Defensive patterns
Strategy: validation
Validate before calling
if (!window.IsVisible) { window.ShowDialog(); } Type guard
bool CanShowDialog(Window w) => !w.IsVisible && !w.IsLoaded || !w.IsVisible;
Try / catch
try { window.ShowDialog(); } catch (InvalidOperationException ex) when (ex.Message == SR.ShowDialogOnVisible) { window.Activate(); } Prevention
- Create a new Window instance per modal session
- Check IsVisible before ShowDialog
- Disable trigger buttons while the dialog is open
When it happens
Trigger: 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.
Common situations: 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.
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
- SR.ShowDialogOnModal
- SR.CannotSetOwnerToItself
- SR.CantSetOwnerAfterDialogIsShown
- SR.CantSetOwnerToClosedWindow
- SR.CantSetOwnerWhosHwndIsNotCreated
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/45b269f16855e6b6.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs:280
/// Shows the window as a modal window
/// </summary>
/// <returns>bool?</returns>
/// <remarks>
/// Callers must have UIPermission(UIPermissionWindow.AllWindows) to call this API.
/// </remarks>
public Nullable<bool> ShowDialog()
{
// this call ends up throwing an exception if ShowDialog
// is not allowed
VerifyApiSupported();
VerifyContextAndObjectState();
VerifyCanShow();
VerifyNotClosing();
VerifyConsistencyWithAllowsTransparency();
if (_isVisible)
{
throw new InvalidOperationException(SR.ShowDialogOnVisible);
}
else if (_showingAsDialog)
{
throw new InvalidOperationException(SR.ShowDialogOnModal);
}
_dialogOwnerHandle = _ownerHandle;
// verify owner handle is window
if (!UnsafeNativeMethods.IsWindow( new HandleRef( null, _dialogOwnerHandle ) ))
{
_dialogOwnerHandle = IntPtr.Zero;
}
// remember the current active window;
// this is used when dialog creation fails or dialog closes, we set the active window back to this one.
_dialogPreviousActiveHandle = UnsafeNativeMethods.GetActiveWindow();View on GitHub (pinned to 81131a70a4)