COM object that has been separated from its underlying RCW cannot be used.

1960
5
02-10-2012 03:58 AM
RogerSteen
New Contributor
How can I avoid "COM object that has been separated from its underlying RCW cannot be used."?

Is this code completely wrong?
I am trying to start listeners for OnEditorStartEditing and OnEditorStopEditing like this:
    Public Sub EditorEvent_Add()
        EditorEvent_Remove()
        Dim pID As esriSystem.UID = New esriSystem.UID : pID.Value = "esriEditor.Editor"

        Try
            m_EditorEventOnStartEditing = New Editor.IEditEvents_OnStartEditingEventHandler(AddressOf OnEditorStartEditing)
            AddHandler CType(CType(m_Application.FindExtensionByCLSID(pID), Editor.IEditor), Editor.IEditEvents_Event).OnStartEditing, m_EditorEventOnStartEditing
        Catch ex As Exception
            MsgBox("Message" & vbNewLine & ex.Message & vbNewLine & "StackTrace" & vbNewLine & ex.StackTrace, MsgBoxStyle.OkOnly, "OnEditorStartEditing")
        End Try
        Try
            m_EditorEventOnStopEditing = New Editor.IEditEvents_OnStopEditingEventHandler(AddressOf OnEditorStopEditing)
            AddHandler CType(CType(m_Application.FindExtensionByCLSID(pID), Editor.IEditor), Editor.IEditEvents_Event).OnStopEditing, m_EditorEventOnStopEditing
        Catch ex As Exception
            MsgBox("Message" & vbNewLine & ex.Message & vbNewLine & "StackTrace" & vbNewLine & ex.StackTrace, MsgBoxStyle.OkOnly, "OnEditorStopEditing")
        End Try
    End Sub
0 Kudos
5 Replies
AlexanderGray
Occasional Contributor III
I had this problem way back in 9.2 when I tried to put event listeners on a non-modal form.  It would work ok for a while then I would get RCW errors.  That was a threading problem with a non-modal form but it could have been any kind of threading problem.
0 Kudos
RogerSteen
New Contributor
I had this problem way back in 9.2 when I tried to put event listeners on a non-modal form.  It would work ok for a while then I would get RCW errors.  That was a threading problem with a non-modal form but it could have been any kind of threading problem.


So. Did you solve the issue?
I want a non-modal form to listen to events, but in 10.
0 Kudos
AlexanderGray
Occasional Contributor III
Yup I solved the issue.  It's a little convoluted and I am sure there are better ways of doing it.
I defined my own custom event type in .net
I added an event handler on the form for my own custom event
I created an ArcObjects extension.
I added the ArcObjects event handlers on the extension
In the extension event handler, I raise the custom extension
That kicks up the event handler on the form.
On the form's custom event handler, I changed the properties of the controls using BeginInvoke.
BeginInvoke has a different way of pumping the messages through windows that lots of online discussions can explain much better than I can.

I am not sure if the custom event was really necessary, I stopped fiddling when I got things to work with the BeginInvoke.  I think calling a public method on the form from the event handler in the extension would have worked as long as you used BeginInvoke on the controls in the public method on the form.  I didn't test that.  In my case I was making a sort of custom identify tool that worked modeless and had to respond to selection changes on the map.  I think I might have the code on some external hard drive at home somewhere but I am in the middle of a move...

I find this topic very interesting and if other people have done this sort of thing, feel free to chime in, I am curious as to how people have handled this sort of problem.  Could make a really nice sample in the advanced topics from programming with ArcObjects.
0 Kudos
RichardWatson
Frequent Contributor
Trying to access ArcObjects across threads does not work and results in the RCW error.

There are a couple of solutions to this. 

One is to not use multiple threads. 

The other is to send the calls from the worker threads back to the main thread in order to avoid cross thread communication.  Windows provides functions to do this.

I had once hoped that ESRI would move towards providing .NET objects which would function across threads but I no longer think that this is the case.  The strategy seems to be to stay with STA COM instead.  Most disappointing.
0 Kudos
AlexanderGray
Occasional Contributor III
Good point Richard, I would only pass literals across thread.  Like an objectid or some string arrays.
0 Kudos