Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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


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?
Posted
Updated 2-Jan-17 19:50pm

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.
 
Share this answer
 
Comments
ielsky 2-Feb-11 12:52pm    
Thank you for you answer.

I checked application log. Thread id in the place where error occurs is the same as thread id in NewInspector handler (where we get Inspector). So, I need another idea.
ncc01 12-Feb-11 3:09am    
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.
You need have strong reference to event.
Anything like this
C#
MailItem mailItem;
CloseEvent closeEvent;
...
InspectorEvents_10_Event inspectorEvents = mailItem.GetInspector;
closeEvent = inspectorEvents.Close;
closeEvent += delegate {.....};
 
Share this answer
 
hey,
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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