0

I'm trying to catch the OnCreateFeature event throughout the lifespan of the application, but ArcMap seems to be "disabling" my event handler when I exit edit mode in ArcMap. If I subscribe the event handler again after ArcMap "disables" it, it re-enables the first handler and subscribes the second one, leaving with my event handler being invoked twice. How can I keep my events subscribed?

This behaviour is really bizarre, but there's nothing else operating on this event.

Here's how I'm currently subscribing my events.

private void HookupEvents()
{
    var events = (IEditEvents_Event)ArcMap.Editor;
    events.OnCreateFeature += Events_OnChangeFeature;
}

Update

So I found that assigning (IEditEvents_Event)ArcMap.Editor to a static variable stops my events from getting unsubscribed. This leads me to suspect some sort of garbage collection/compiled optimization issue.

1

Where do you wire the events? Which ArcGIS version are you using?

I’m wiring the event in the OnStartup() method of my extension before any edit session is started. This works quite fine. Stopping and restarting the edit session keeps the one and only wired event handler – as expected – alive. My code looks as follows:

private void WireEvents()
{
    //Get a reference to the editor.
    UID uid = new UIDClass();
    uid.Value = "esriEditor.Editor";
    IEditor m_Editor = ArcMap.Application.FindExtensionByCLSID(uid) as IEditor;

    IEditEvents_Event editEvents = m_Editor as IEditEvents_Event;
    if (editEvents != null)
    {
        editEvents.OnCreateFeature += new IEditEvents_OnCreateFeatureEventHandler(EventListener_OnCreateFeature);
    }
}

private void EventListener_OnCreateFeature(object theObject)
{
}
  • I'm using ArcMap 10.4.1. I'll try adding the OnStartup method and adding the handler in there but where does the OnStartup Method go? – The Pademelon Sep 19 '16 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.