MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException

Dialog session has ended.

Error message

Dialog session has ended.

What it means

InvalidOperationException('Dialog session has ended.') from DialogSession.Close() (DialogSession.cs:53). Once IsEnded is true (set during the closing sequence), no further Close calls are accepted. Calling Close() after the dialog already closed throws.

Source

Thrown at src/MaterialDesignThemes.Wpf/DialogSession.cs:53

    /// </summary>
    /// <param name="content"></param>
    public void UpdateContent(object? content)
    {
        _owner.AssertTargetableContent();
        _owner.DialogContent = content;
        _owner.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
        {
            _owner.FocusPopup();
        }));
    }

    /// <summary>
    /// Closes the dialog.
    /// </summary>
    /// <exception cref="InvalidOperationException">Thrown if the dialog session has ended, or a close operation is currently in progress.</exception>
    public void Close()
    {
        if (IsEnded) throw new InvalidOperationException("Dialog session has ended.");

        _owner.InternalClose(null);
    }

    /// <summary>
    /// Closes the dialog.
    /// </summary>
    /// <param name="parameter">Result parameter which will be returned in <see cref="DialogClosingEventArgs.Parameter"/> or from <see cref="DialogHost.Show(object)"/> method.</param>
    /// <exception cref="InvalidOperationException">Thrown if the dialog session has ended, or a close operation is currently in progress.</exception>
    public void Close(object? parameter)
    {
        if (IsEnded) throw new InvalidOperationException("Dialog session has ended.");

        _owner.InternalClose(parameter);
    }
}

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Guard with 'if (!session.IsEnded) session.Close();' before closing.
  2. Disable the close button/command after first invocation.
  3. Avoid calling session.Close from within DialogClosing/DialogClosed handlers where the session is already ending.

Example fix

// before
private void CloseClick() => _session.Close(); // throws on second click

// after
private void CloseClick()
{
    if (_session is { IsEnded: false } s)
        s.Close();
}
Defensive patterns

Strategy: validation

Validate before calling

if (session is { IsEnded: false })
    session.Close();

Type guard

static bool CanCloseSession(DialogSession? session) => session is { IsEnded: false };

Try / catch

try
{
    session.Close();
}
catch (InvalidOperationException ex) when (ex.Message == "Dialog session has ended.")
{
    // already closed; ignore
}

Prevention

When it happens

Trigger: Invoking session.Close() after the session has ended: double-close, closing from a timer/command that fires after dismissal, or calling Close from inside a closing callback that already ended the session.

Common situations: Rapid double-click on a close button whose command calls session.Close; delayed/async close logic firing after the dialog is gone; close from a background dispatcher tick.

Related errors


AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13). Data as JSON: /api/errors/d0abfbdaaf9a82e1. Report an issue: GitHub.