Skip to content

Fix COM threading issue in Office interop examples (HRESULT 0x8001010A) #47050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 2, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 1, 2025

The Office interop code examples were causing COM exceptions due to threading and timing issues when Word was made visible during COM operations. This resulted in the error:

System.Runtime.InteropServices.COMException: The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A)

Changes Made

Fixed the timing pattern in all Word interop methods:

  • Changed wordApp.Visible = true at the beginning to wordApp.Visible = false
  • Added wordApp.Visible = true at the end after all COM operations complete
  • Added explanatory comments about avoiding COM threading issues

Updated methods:

  • Main DisplayInWord() method in OfficeHowTo namespace
  • DisplayInWord() method in Parts namespace (Snippet6)
  • VS2008() method in Parts namespace

Enhanced documentation:

  • Added a NOTE explaining the COM threading issue and solution approach
  • Clarified why Word is kept invisible during operations

Technical Background

The HRESULT 0x8001010A error occurs when COM operations are performed on a visible Word application due to message filtering conflicts between the host application and Word's UI thread. By keeping Word invisible during operations, we eliminate the timing window where these conflicts occur.

This approach is more reliable than alternatives like Thread.Sleep() (unpredictable timing) or implementing a custom MessageFilter (overly complex for documentation examples).

Before/After Example

Before (problematic):

var wordApp = new Word.Application();
wordApp.Visible = true;  // Can cause COM exception
var doc = wordApp.Documents.Add();
// ... perform operations while visible

After (fixed):

var wordApp = new Word.Application();
wordApp.Visible = false;  // Keep invisible during operations
var doc = wordApp.Documents.Add();
// ... perform all operations safely
wordApp.Visible = true;   // Show result when complete

The fix maintains all original functionality and educational value while making the examples stable and reliable.

Fixes #18862.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/csharp/advanced-topics/interop/how-to-use-named-and-optional-arguments-in-office-programming.md docs/csharp/advanced-topics/interop/how-to-use-named-and-optional-arguments-in-office-programming

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Example with standard Error System.Runtime.InteropServices.COMException: HRESULT: 0x8001010A Fix COM threading issue in Office interop examples (HRESULT 0x8001010A) Jul 1, 2025
@Copilot Copilot AI requested a review from BillWagner July 1, 2025 18:11
Copilot finished work on behalf of BillWagner July 1, 2025 18:11
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

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

You need to edit the snippets.5000.json file in the snippets/NamedAndOptional folder to change the error location from line 8 to line 5 (see line 6 of the json file)

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 2, 2025

You need to edit the snippets.5000.json file in the snippets/NamedAndOptional folder to change the error location from line 8 to line 5 (see line 6 of the json file)

Updated the error location in snippets.5000.json from line 8 to line 5 to correctly point to the using Word = Microsoft.Office.Interop.Word; statement. Commit: 84eaaa8

@Copilot Copilot AI requested a review from BillWagner July 2, 2025 13:38
Copilot finished work on behalf of BillWagner July 2, 2025 13:38
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

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

Marking this ready for final review.

Tagging @adegeo to look at the expected errors and snippets 5000. This looks right to me, but that expected error is still causing a failure.

@BillWagner BillWagner requested review from IEvangelist and adegeo July 2, 2025 13:55
@BillWagner BillWagner marked this pull request as ready for review July 2, 2025 13:55
@BillWagner BillWagner requested a review from a team as a code owner July 2, 2025 13:55
@BillWagner BillWagner enabled auto-merge (squash) July 2, 2025 13:56
@BillWagner BillWagner disabled auto-merge July 2, 2025 14:43
@BillWagner BillWagner merged commit 548fc55 into main Jul 2, 2025
9 of 10 checks passed
@BillWagner BillWagner deleted the copilot/fix-18862 branch July 2, 2025 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Example with standard Error System.Runtime.InteropServices.COMException: HRESULT: 0x8001010A
3 participants