Skip to content

Fix UIA Invoke hang when ToolStripMenuItem opens modal dialog from MenuStrip - #14301

Open
LeafShi1 wants to merge 2 commits into
dotnet:mainfrom
LeafShi1:Fix_10244_InvokePattern_blocks_on_menuItem
Open

Fix UIA Invoke hang when ToolStripMenuItem opens modal dialog from MenuStrip#14301
LeafShi1 wants to merge 2 commits into
dotnet:mainfrom
LeafShi1:Fix_10244_InvokePattern_blocks_on_menuItem

Conversation

@LeafShi1

@LeafShi1 LeafShi1 commented Feb 14, 2026

Copy link
Copy Markdown
Member

Fixes #10244

Root Cause

  • UI Automation invokes menu items via ToolStripMenuItemAccessibleObject.Invoke, which previously called DoDefaultAction() synchronously.
  • When the menu item lives on a MenuStrip and its click handler opens a modal window (e.g. ShowDialog / MessageBox.Show), this happens while WinForms is inside the Win32 “menu mode” message loop.
  • The synchronous call starts a nested modal loop inside the menu loop while the UIA client is still blocked waiting for Invoke to return, which can lead to the invoke call appearing “stuck” or the app becoming unresponsive in this scenario.

Proposed changes

  • Override ToolStripMenuItemAccessibleObject.Invoke to dispatch the default action asynchronously when the item has an owning ToolStrip with a created handle:
    • If Owner is non-null and Owner.IsHandleCreated is true, call owner.BeginInvoke(new MethodInvoker(DoDefaultAction)); and return immediately.
    • If there is no owner (or no handle yet), fall back to calling DoDefaultAction() directly as before.
  • This ensures that for real MenuStrip scenarios the click logic (and any modal UI it shows) runs after the menu loop has exited, under the normal UI message pump, while non-attached/test-only items preserve their existing synchronous behavior.

Customer Impact

  • UI Automation tools invoking MenuStrip menu items that open modal dialogs will no longer hang or throw errors, so automated testing and accessibility scenarios can reliably trigger these commands.

Regression?

  • Yes

Risk

  • Minimal

Screenshots

Before

Accessibility tools and UIA-based automation that invoke ToolStripMenuItem items which open dialogs from a MenuStrip will blocked, invokes will fail to complete.

BeforeChanges.mp4

After

Accessibility tools and UIA-based automation that invoke ToolStripMenuItem items which open dialogs from a MenuStrip will no longer hang or appear blocked, invokes will complete and dialogs will show reliably.

AfterChanges.mp4

Test methodology

  • Unit test

Test environment(s)

  • .net 11.0.0-preview.2.26080.101
Microsoft Reviewers: Open in CodeFlow

@Shyam-Gupta

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@codecov

codecov Bot commented Feb 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.23985%. Comparing base (9b546b7) to head (43b3a46).
⚠️ Report is 167 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #14301         +/-   ##
===================================================
+ Coverage   77.18395%   77.23985%   +0.05590%     
===================================================
  Files           3279        3279                 
  Lines         645138      645132          -6     
  Branches       47730       47730                 
===================================================
+ Hits          497943      498299        +356     
+ Misses        143503      143126        -377     
- Partials        3692        3707         +15     
Flag Coverage Δ
Debug 77.23985% <100.00000%> (+0.05590%) ⬆️
integration 19.08742% <0.00000%> (+0.09678%) ⬆️
production 52.16967% <100.00000%> (+0.10648%) ⬆️
test 97.42121% <100.00000%> (+0.01641%) ⬆️
unit 49.55251% <100.00000%> (+0.04988%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LeafShi1 LeafShi1 added the waiting-review This item is waiting on review by one or more members of team label Apr 22, 2026
@LeafShi1
LeafShi1 requested a lite review from Copilot August 14, 2026 07:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a UI Automation (UIA) hang when InvokePattern.Invoke is used on a ToolStripMenuItem hosted by a MenuStrip whose click handler opens modal UI (e.g., ShowDialog). The fix changes the UIA invoke path to dispatch the default action asynchronously when the owning ToolStrip has a created handle, avoiding nested modal/message loops that can make the UIA client appear blocked.

Changes:

  • Override ToolStripMenuItemAccessibleObject.Invoke() to BeginInvoke(DoDefaultAction) when the owning ToolStrip handle is created; otherwise fall back to synchronous DoDefaultAction().
  • Re-enable and update the previously-skipped unit test scenario that opens a modal dialog from a MenuStrip item.
  • Add a new unit test asserting the invoke behavior is asynchronous when the item has an owner with a handle.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/System.Windows.Forms/System/Windows/Forms/Controls/ToolStrips/ToolStripMenuItem.ToolStripMenuItemAccessibleObject.cs Implements asynchronous UIA invoke dispatch via BeginInvoke when hosted by a handle-created ToolStrip.
src/test/unit/System.Windows.Forms/System/Windows/Forms/AccessibleObjects/ToolStripMenuItem.ToolStripMenuItemAccessibleObjectTests.cs Updates/regresses the test that previously hung and adds coverage for the new asynchronous behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Infrastructure waiting-review This item is waiting on review by one or more members of team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.Windows.Automation.InvokePattern.Invoke blocks on a menu item.

3 participants