[GIS] How to list ActiveView SelectionChanged event delegates in ArcObjects .NET

add-inarcmaparcobjectsevents

I currently have an issue in an ArcObjects ArcMap add-in where my event handlers are seemingly-randomly being unhooked from the event I'm trying to catch. I've spent some time looking for a way to list all delegates currently assigned for the event but I can't find a way to call Delegate.GetInvocationList() from the event interface.

When my add-in is created I execute the following to respond to the ActiveView SelectionChanged event:

IActiveViewEvents_Event activeViewEvent = (IActiveViewEvents_Event)ArcMap.Document.FocusMap;
activeViewEvent.SelectionChanged += new IActiveViewEvents_SelectionChangedEventHandler(this.onSelectionChanged);

Now I know that this event handler will be lost when the active view changes so I also have this:

IDocumentEvents_Event documentEvent = (IDocumentEvents_Event)ArcMap.Document;
documentEvent.ActiveViewChanged += new IDocumentEvents_ActiveViewChangedEventHandler(this.onActiveViewChanged);

which calls the previous code to re-add the SelectionChanged event handler.

At seemingly random intervals my application stops responding to the SelectionChanged event. I'm stopping on all CLR errors so I'm sure it's not a silent exception that's disabling my add-in.

What I'd really like is to be able to inspect the delegates assigned to the SelectionChanged event and see if mine needs re-adding, but so far no joy.

Any suggestions on what might be going wrong, or how to inspect the assigned event handlers would be VERY much appreciated!

EDIT:

I've since figured out a pattern to the madness. My tool will only run within an edit session if features are selected. One way I can replicate this behaviour is to
1) start an edit session
2) select some features
3) run my tool – copies selected features from one layer to another
4) stop editing (it doesn't matter whether I save my edits or not)
5) Now the selection changed event is no longer handled

Can anyone tell me why starting and then stopping editing would detach my selection changed event handlers?

Best Answer

Thanks for the interest on this question. I ended up using a different approach that actually works better in my situation and is more reliable.

Instead of using IActiveViewEvents::SelectionChanged I went with IEditEvents::SelectionChanged as I actually only needed to know if the selection changed while editing. As the editor object never changes the event handler is never lost so there's no need to reassign the handler.

I spent a while longer testing the original issue and I'm fairly confident it's an ArcObjects issue as I wasn't actually able to replicate it every time using the exact same sequence of events.

I'd still like to know if the list of delegates can be retrieved for an ArcObjects event but for now my original problem is solved.

Related Question