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!
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?