You are here: > ESRI Forums > arcgis engine forums > Thread Replies

ArcGIS Engine Forums

ArcGIS Engine: ArcObjects .NET API forum

MapControl loses ActiveView events after la...   Randal Greene Sep 21, 2009
Re: MapControl loses ActiveView events afte...   Randal Greene Sep 21, 2009
Re: MapControl loses ActiveView events afte...   Franck Krogh Mortensen Sep 22, 2009
Re: MapControl loses ActiveView events afte...   Randal Greene Sep 22, 2009
Re: MapControl loses ActiveView events afte...   Franck Krogh Mortensen Sep 22, 2009
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject MapControl loses ActiveView events after layers added 
Author Randal Greene 
Date Sep 21, 2009 
Message Good day,

I'm building an ArcGIS Engine for 9.3 using VS.NET 2008 (C#). In OnMapReplaced I hook up ActiveView event handlers for ContentsChanged and ItemAdded. Later, adding a newly created raster layer using InsertLayerInGroup causes the ActiveView event handlers to usually (but not always) be unhooked. What would cause this? Why is it inconsistent? What can be done about it?

Attempting to re-hook the ActiveView event handlers results in "COM object that has been separated from its underlying RCW cannot be used". However, I think the fist paragraph is the real issue!

Thanks in advance... 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: MapControl loses ActiveView events after layers added 
Author Randal Greene 
Date Sep 21, 2009 
Message Did some more testing and it appears that adding the new layer was not what caused the events to unhook. Simply turning layers on/off and right-clicking them to show the context menu eventually caused the events to unhook.

So I did some more hunting on the forum here, and found several references to use of static variables. So I made the delegates static variables, and thought I had solved the problem entirely. But a few minutes ago, after a relatively long test session, the ActiveView events became unhooked!

Any thoughts appreciated... 
 
static IActiveViewEvents_ContentsChangedEventHandler m_ContentsChangedEventHandler;
static IActiveViewEvents_ItemAddedEventHandler m_ItemAddedEventHandler;

private void m_AxMapControl_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
{
	m_ContentsChangedEventHandler = new IActiveViewEvents_ContentsChangedEventHandler(m_AxMapControl_ActiveView_ContentsChanged);
	((ESRI.ArcGIS.Carto.IActiveViewEvents_Event)(m_AxMapControl.ActiveView)).ContentsChanged += m_ContentsChangedEventHandler;
	m_ItemAddedEventHandler = new IActiveViewEvents_ItemAddedEventHandler(m_AxMapControl_ActiveView_ItemAdded);
	((ESRI.ArcGIS.Carto.IActiveViewEvents_Event)(m_AxMapControl.ActiveView)).ItemAdded += m_ItemAddedEventHandler;
}
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: MapControl loses ActiveView events after layers added 
Author Franck Krogh Mortensen 
Date Sep 22, 2009 
Message I think if you make the ((ESRI.ArcGIS.Carto.IActiveViewEvents_Event)(m_AxMapControl.ActiveView)) as a member variable, your problem can be solved. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: MapControl loses ActiveView events after layers added 
Author Randal Greene 
Date Sep 22, 2009 
Message So far so good with my testing.

Does only IActiveViewEvents_Event need to be a member, or the EventHandlers as well?

Does it matter whether I use static members or instance members?

Thanks for your help. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: MapControl loses ActiveView events after layers added 
Author Franck Krogh Mortensen 
Date Sep 22, 2009 
Message It is only the IActiveViewEvents_Event that need to be a member.

When we saw the problem we concluded that somehow the IActiveViewEvents_Event is marked for garbage collection when used as a local variable (and that's why the events gets lost).