Microsoft COM加载项的多线程问题

时间:2013-10-20 07:44:28

标签: c# multithreading com office-addins

我正在为Microsoft Office产品创建加载项,并且只想在后台执行耗时的任务。在我的情况下办公室程序有更多内容,更多的时间插件需要完成它的工作。因此,我需要在用户执行其他任务时向后台执行此类作业,或向用户显示进度条,以显示加载项在主线程中工作时还有多少时间完成其作业。我发现Microsoft Office对象模型不是线程安全的,并且不是从COM加载项的主线程访问对象,属性或方法会产生异常,并且不建议使用与COM组件交互的线程。但我不想让用户等待加载项正在完成其工作而不知道卡住屏幕后面发生了什么。有没有办法使用COM加载项的多线程。

编辑: 我试过backgroundWorker。但它会产生这样的异常:

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.

2 个答案:

答案 0 :(得分:0)

取决于您尝试做什么和您的办公室版本,有几种解决方案。我发现这些很有用:

Office中的线程支持: http://msdn.microsoft.com/en-us/library/vstudio/8sesy69e(v=vs.100).aspx

组件中的多线程: http://msdn.microsoft.com/en-us/library/vstudio/3es4b6yy(v=vs.100).aspx

答案 1 :(得分:-1)

尝试使用此功能:

DisableComObjectEagerCleanup()