0

我正在为 Microsoft Office 产品创建加载项,只想在后台执行耗时的任务。在我的情况下,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.
4

2 回答 2

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

于 2013-10-20T08:16:16.877 回答
-1

尝试使用此功能:

DisableComObjectEagerCleanup()
于 2015-10-22T12:20:13.883 回答