Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I'm automating Word and when I call a function that doesn't return for a long time (for example a call to
app.Run(_T("FilePrint"));) it tends to freeze the UI of my app until it returns from the call, so I tried to run this code from a worker thread, but then COM complains and throws an exception RPC_E_WRONG_THREAD - "0x8001010E: The application called an interface that was marshalled for a different thread."

How can I solve this problem?




btw, there's an explanation about RPC_E_WRONG_THREAD in MSDN - INFO: Explanation of RPC_E_WRONG_THREAD Error[^], but I couldn't figure out anything from it.

Posted

Could you do all of the automation, including creation of the Word object, on the worker thread?

Basically you're running foul of COM single-threaded apartments and things, I think. This article[^] explains them well.

 
Share this answer
 
You need to marshal calls across threads/apartments. See this[^] article for different ways to perform cross thread marshalling.

CoMarshalInterThreadInterfaceInStream() would probably be the easiest approach. Look it up in the MSDN documentation. It's quite easy to use.
 
Share this answer
 
v2


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900