“不能使用已与其基础RCW分离的COM对象”。例外 [英] "COM object that has been separated from its underlying RCW cannot be used" exception

查看:78
本文介绍了“不能使用已与其基础RCW分离的COM对象”。例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我的申请中有一些奇怪的情况(MS Outlook Addin)。



我在Item.Open事件处理程序中使用MailItem对象。

我得到Inspector对象调用MailItem.GetInspector属性。

当我尝试为Inspector.Close事件设置处理程序时抛出InvalidComObjectException,并显示消息已与基础RCW分离的COM对象无法使用。



代码段:

Hello Everybody.

I have strange situation in my applicaion (MS Outlook Addin).

I work with MailItem object in Item.Open event handler.
I get Inspector object calling MailItem.GetInspector property.
When I try to set handler for Inspector.Close event InvalidComObjectException with message "COM object that has been separated from its underlying RCW cannot be used" is thrown.

Code snippet:

MailItem mailItem;
...
InspectorEvents_10_Event inspectorEvents = mailItem.GetInspector;
inspectorEvents.Close += delegate {.....}; //Exception occurs





在我看来,这是不可能的情况。获取Inspector并使用它之间没有任何 Marshal.ReleaseComObject 的调用。



此错误非常复制很少,所以在调试时我无法抓住它。



有什么建议吗?



In my opinion, it is impossible situation. There is no any call of Marshal.ReleaseComObject between getting Inspector and using it.

This error is reproduced very rarely, so I can't catch it while debugging.

Any suggestions?

推荐答案

一个可能原因是您从不同的线程访问单线程COM对象。在您的情况下,事件处理程序可能没有在创建COM对象的线程上运行。
One likely cause is that you are accessing a single threaded COM object from different threads. In your case, the event handler may not be running on the thread that created the COM object.


您需要强烈引用事件。

这样的事情

You need have strong reference to event.
Anything like this
MailItem mailItem;
CloseEvent closeEvent;
...
InspectorEvents_10_Event inspectorEvents = mailItem.GetInspector;
closeEvent = inspectorEvents.Close;
closeEvent += delegate {.....};


嘿,
Hi, I am facing the same problem it is really difficult to debug it because the test case is not unique. did you find a solution? I really appreciate your help in advance.Here is my mailbox:1424870395@qq.com


这篇关于“不能使用已与其基础RCW分离的COM对象”。例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆