Multi-threading issue with Microsoft COM Add-ins

0

I'm creating add-in for Microsoft office products and just want to execute time consumable tasks on background. In my situation more contents office program has, more time add-in requires to done its job. So I need to execute a such job in background while user is doing other tasks or show progress bar to user showing how much time left to add-in finishes its job while it works in main thread. I found out that Microsoft Office Object Model is not thread safe and accessing object, property or method not from the main thread of a COM add-in gives exception and not suggested to use threads that interacts with COM's components. But I don't want to user wait while add-in is doing its job not knowing what's going on behind stuck screen. Is there any way to use multi-threading with COM add-in.

Edit: I tried backgroundWorker. But it results exception like this:

DisconnectedContext was detected.
Transition into COM context 0x8f1890 for this RuntimeCallableWrapper failed with the following
error: An outgoing call cannot be made since the application is dispatching an input-synchronous
call. (Exception from HRESULT: 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL)). This is 
typically because the COM context 0x8f1890 where this RuntimeCallableWrapper was created has 
been disconnected 
or it is busy doing something else and cannot process the context transition. No proxy will be       
used 
to service the request on the COM component and calls will be made to the COM component directly. 
This may cause corruption or data loss. To avoid this problem, please ensure that all COM 
contexts/apartments/threads stay alive and are available for context transition, until the 
application is completely done with the RuntimeCallableWrappers that represents COM components that 
live inside them.
c#
multithreading
com
office-addins
asked on Stack Overflow Oct 20, 2013 by Odgiiv • edited Oct 29, 2015 by pnuts

2 Answers

0

Depends on what you trying to do and your office version, there are several solutions. I've found these useful:

Threading Support in Office: http://msdn.microsoft.com/en-us/library/vstudio/8sesy69e(v=vs.100).aspx

Multithreading in Components: http://msdn.microsoft.com/en-us/library/vstudio/3es4b6yy(v=vs.100).aspx

answered on Stack Overflow Oct 20, 2013 by etaiso
-1

Try use this function:

DisableComObjectEagerCleanup()
answered on Stack Overflow Oct 22, 2015 by Mohamed Adel • edited Oct 23, 2015 by slavoo

User contributions licensed under CC BY-SA 3.0