Sporadic RPC_E_WRONG_THREAD on Dispose()

Apr 27, 2015 at 8:58 PM
Hi,

I wanted to thank you for NetOffice, it is a truly fantastic piece of work.

I'm encountering the following error but it's very sporadic and has only happened a couple of times:
Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Runtime.InteropServices.ComTypes.IConnectionPoint'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B196B286-BAB4-101A-B69C-00AA00341D07}' failed due to the following error: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)).

at NetOffice.SinkHelper.RemoveEventBinding(Boolean removeFromList)
at NetOffice.SinkHelper.Dispose()
at NetOffice.OutlookApi.Explorer.DisposeEventBridge()
at NetOffice.ComObject.Dispose(Boolean disposeEventBinding)
at NetOffice.ComObject.Dispose()
My code is:
private void AddinShutdown()
        {
                if (this.outlookMailItem != null)
                {
                    this.outlookMailItem.Dispose();
                }

                if (this.outlookExplorer != null)
                {
                    this.outlookExplorer.Dispose();
                }

                if (this.outlookExplorers != null)
                {
                    this.outlookExplorers.Dispose();
                }

                if (this.outlookInspector != null)
                {
                    this.outlookInspector.Dispose();
                }

                if (this.outlookInspectors != null)
                {
                    this.outlookInspectors.Dispose();
                }

                if (this.outlookApplication != null)
                {
                    this.outlookApplication.Dispose();
                }

                if (this.ctpFactory != null)
                {
                    this.ctpFactory.Dispose();
                }
Have you encountered this before and am I doing anything wrong in my code to make this happen?
Coordinator
May 5, 2015 at 4:28 PM
i have no idea which line of code is the problem. I see its in Dispose but you call it multiple times(not sure why because its typical not necessary in NetOffice)
RPC_E_WRONG_THREAD indicates AddinShutdown is running in the wrong thread. I need definitely more details.
May 5, 2015 at 4:57 PM
Thanks Sebatian. I'm afraid I don't have any more details other than a screenshot of the exception that the user sent me when they encountered this error which gives no more info than what I've put above. I haven't succeeded in recreating it myself.

I'm simply disposing of any open COM variables. If this is done by NetOffice automatically, I'll remove all of them.
May 12, 2015 at 8:17 AM
Do you have any initialization logic in background thread? Do you call office API in background thread? Or any other threads? Does any code calling COM objects run in finalizers? Are there other add-in running on user machine?

Actually I may point articles about COM/RPC/STA/marshaling/threading which I used to learn root causes of such errors.
May 12, 2015 at 8:18 AM
Your stack is not full. I mean why AddinShutdown is not in it but starts with NetOffice.ComObject.Dispose()?
May 12, 2015 at 8:33 AM
My stack trace is missing one line - at ProductName.Addin.AddinShutdown() - I hand typed the rest of it from a screenshot sent by the user...
In answer to the questions:
Do you have any initialization logic in background thread? Yes, thanks for the pointer. I've discovered that I'm setting this.outlookMailItem in a BackgroundWorker:
this.outlookMailItem = (Outlook.MailItem)selectedItem;
Do you call office API in background thread? Or any other threads? Yes, I use a BackgroundWorker for some code that extracts the data from the currently selected email as I found that having it run on the main thread was slowing down Outlook.
Does any code calling COM objects run in finalizers? No
Are there other add-in running on user machine? Yes, the user tells me that he uses the Dragon Naturally Speaking add in to record voice into emails.