dotnet/wpf · error · InvalidOperationException
(no message) InvalidOperationException()
Error message
(no message) InvalidOperationException()
What it means
InputManager.PopMenuMode throws a parameterless InvalidOperationException when _menuModeCount is zero or negative, meaning PopMenuMode was called more times than PushMenuMode. Menu mode is a ref-counted nesting; popping without a matching push breaks the internal invariant that the stack of menu sites is balanced.
Solutions
- Balance every PopMenuMode with exactly one PushMenuMode; track your own nesting count if paths can overlap.
- Check that cleanup/dispose logic does not call PopMenuMode redundantly (use a pushed flag).
- Catch InvalidOperationException around the pop as a defensive guard in interop-heavy code, but fix the imbalance.
Example fix
// before closeHandler += () => site.PopMenuMode(); // may run twice // after if (!_menuPushed) return; _menuPushed = false; site.PopMenuMode();
Defensive patterns
Strategy: try-catch
Validate before calling
if (_menuModeCount <= 0) return; // track your own push count alongside InputManager's
Try / catch
try { inputManager.PopMenuMode(menuSite); }
catch (InvalidOperationException) { /* unbalanced pop; log and continue */ } Prevention
- Maintain your own boolean/count so each PushMenuMode is popped exactly once.
- Guard cleanup paths so error and close handlers do not both pop.
- In menu interop, handle WM_EXITMENULOOP idempotently.
When it happens
Trigger: Calling PopMenuMode on an HwndSource/InputManager pair without a preceding PushMenuMode, or calling it twice after one push, e.g. in custom popup/menu interop code or double-dispose logic.
Common situations: Custom menu/popup hosting code that pushes on one path but pops on several (error + cleanup paths both popping); re-entrant close handlers; interop with Win32 menus where WM_EXITMENULOOP arrives more than once.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- ' ' can only host a ' ' or a ' '. ' ' is an invalid…
- ' ' can only host a ' ' or a ' '. ' ' is an invalid…
- InvalidEnumArgumentException("action", (int)action…
- InvalidEnumArgumentException("actions", (int)actions…
- InvalidEnumArgumentException("actions", (int)actions…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/996b43c1d1c12b30.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputManager.cs:353
EventHandler enterMenuMode = EnterMenuMode;
if (null != enterMenuMode)
{
enterMenuMode(null, EventArgs.Empty);
}
}
}
/// <summary>
/// Controls call this to leave menu mode.
///</summary>
public void PopMenuMode(PresentationSource menuSite)
{
ArgumentNullException.ThrowIfNull(menuSite);
menuSite.VerifyAccess();
if (_menuModeCount <= 0)
{
throw new InvalidOperationException();
}
menuSite.PopMenuMode();
_menuModeCount -= 1;
if (0 == _menuModeCount)
{
EventHandler leaveMenuMode = LeaveMenuMode;
if (null != leaveMenuMode)
{
leaveMenuMode(null, EventArgs.Empty);
}
}
}
/// <summary>
/// Returns whether or not the input manager is in menu mode.
///</summary>View on GitHub (pinned to 81131a70a4)