m_actViewEvents = CType(m_map, IActiveViewEvents_Event) Trace.WriteLine("active view events retrieved") AddHandler m_actViewEvents.ItemAdded, AddressOf ItemAdded 'exception is thrown here Trace.WriteLine("item added event handler added")
Hello Forum,
I have this intermittent problem, (happens about 60% of the time.)
I have an extension (not addin) where I listen for a start editing event
In the start editing I start other events listeners, specificaly activeviewevents item added, item removed and mapevents featureclass changed.
The problem is often (60% of the time but never in debug mode) I get an exception when adding the event handler
System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.
this is a snippet in the startediting event handlerm_actViewEvents = CType(m_map, IActiveViewEvents_Event) Trace.WriteLine("active view events retrieved") AddHandler m_actViewEvents.ItemAdded, AddressOf ItemAdded 'exception is thrown here Trace.WriteLine("item added event handler added")
I set the m_map in the start editing. I tried casting the m_Editor.map to the Iactiveviewevents (better design), I tried setting the m_map from the editor, I tried using the document focus map. I tried declaring the m_Editor and m_map member vars as shared (static) I tried declaring the m_activeViewEvents as shared.
As far as I know everything is done on the main thread.
I would rather not populate the m_map variable before the edit session is started since the focus map could change.
Public Sub StopEditing(ByVal save As Boolean) Try If m_isInSaveEdit = False AndAlso _ m_editor.EditWorkspace.Type = esriWorkspaceType.esriLocalDatabaseWorkspace Then RemoveHandler m_mapEvents.FeatureClassChanged, AddressOf FeatureClassChanged RemoveHandler m_actViewEvents.ItemAdded, AddressOf ItemAdded RemoveHandler m_actViewEvents.ItemDeleted, AddressOf ItemRemoved RemoveHandler m_IceLineObjectClassEvents.OnChange, AddressOf LineChange RemoveHandler m_IceLineObjectClassEvents.OnDelete, AddressOf LineChange RemoveHandler m_IcePointObjectClassEvents.OnChange, AddressOf PointChange RemoveHandler m_IcePointObjectClassEvents.OnDelete, AddressOf PointChange RemoveHandler m_AOIObjectClassEvents.OnDelete, AddressOf AOIDelete RemoveHandler m_AOIObjectClassEvents.OnCreate, AddressOf AOICreate DisableDockEditorWindows() End If Catch ex As Exception Trace.WriteLine(ex) End Try End Sub
RemoveHandler is being called in the stop editing event.Public Sub StopEditing(ByVal save As Boolean) Try If m_isInSaveEdit = False AndAlso _ m_editor.EditWorkspace.Type = esriWorkspaceType.esriLocalDatabaseWorkspace Then RemoveHandler m_mapEvents.FeatureClassChanged, AddressOf FeatureClassChanged RemoveHandler m_actViewEvents.ItemAdded, AddressOf ItemAdded RemoveHandler m_actViewEvents.ItemDeleted, AddressOf ItemRemoved RemoveHandler m_IceLineObjectClassEvents.OnChange, AddressOf LineChange RemoveHandler m_IceLineObjectClassEvents.OnDelete, AddressOf LineChange RemoveHandler m_IcePointObjectClassEvents.OnChange, AddressOf PointChange RemoveHandler m_IcePointObjectClassEvents.OnDelete, AddressOf PointChange RemoveHandler m_AOIObjectClassEvents.OnDelete, AddressOf AOIDelete RemoveHandler m_AOIObjectClassEvents.OnCreate, AddressOf AOICreate DisableDockEditorWindows() End If Catch ex As Exception Trace.WriteLine(ex) End Try End Sub