Prevent OnChangeFeature event from firing within itself?

1260
6
Jump to solution
04-22-2014 10:12 AM
EvanBlaisdell
New Contributor III
Hello,

I have an OnChangeFeature event wired to fire whenever a feature is changed (obviously).  Within the event, I am updating a field value, which then fires the event again.  Hence, infinite loop.  How can I prevent this from happening?

I have tried to remove the event within itself by using:

editEvents.OnChangeFeature -= new IEditEvents_OnChangeFeatureEventHandler(OnChangeFeature);


...which works, but only if I don't add the event back (with a +=) afterward, which I need to do.  If I add the event back, even after the field update, the event fires again.

Thanks,
Evan.
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Make sure you're not calling IFeature.Store in the event as this will re-fire the behavior. Its called internally once the event is complete.

  public class EditorExtension1 : ESRI.ArcGIS.Desktop.AddIns.Extension   {      private IEditEvents_Event _editEvents;     private int i = 0;      public EditorExtension1()     {     }      protected override void OnStartup()     {       IEditor theEditor = ArcMap.Editor;       _editEvents = theEditor as IEditEvents_Event;       _editEvents.OnChangeFeature += new IEditEvents_OnChangeFeatureEventHandler(_editEvents_OnChangeFeature);     }      void _editEvents_OnChangeFeature(ESRI.ArcGIS.Geodatabase.IObject obj)     {       IFeature feature = obj as IFeature;       feature.set_Value(feature.Fields.FindField("point_x"), (i = i + 1));     }

View solution in original post

0 Kudos
6 Replies
LeoDonahue
Occasional Contributor III
Hello,I have an OnChangeFeature event wired to fire whenever a feature is changed (obviously).  Within the event, I am updating a field value, which then fires the event again.  Hence, infinite loop.  How can I prevent this from happening?
This is an application logic issue.  Are there no other options for you to not attempt a field update within that event?Can you explain your use case for needing to do this in the event?
0 Kudos
EvanBlaisdell
New Contributor III
This is an application logic issue.  Are there no other options for you to not attempt a field update within that event?Can you explain your use case for needing to do this in the event?


I want to update a text field to indicate the fact that the feature has been changed, no matter how it was changed, e.g. via a split, merge, moving a vertex, etc.  I know there is Editor Tracking but that won't work in my situation due to unrelated issues.

Thanks.
0 Kudos
LeoDonahue
Occasional Contributor III
What are you recording in the text field?  Something like: 
"This feature changed on: 2014-04-22 13:54:00"  Changed how?

What to you signals the change event you want to record?  Everything?

Such as:  every time I move a vertex, you want the text field updated, but at some point that field will get over written with the most recent change event.  So what are you planning on putting in the text field?

The ESRI sample for edit events only prints information.  It doesn't try to update a feature within a OnChageFeatureEvent.  At the least, your event is going to fire a minimum of two times for every change.  Once for the change you log, and once for the change you make even outside of the change event and you will run into the same problem with an additional method to handle the text field update.  You'll have to come up with a hack to prevent re-logging the same event.

There are three different events for vertexes alone.  Added, deleted and moved.

Maybe you need to implement the IEngineEditEvents in your code that signal the event you really want to record.  My guess is OnChangeFeature was meant to log this information somewhere else, not in the featureclass you are changing.
0 Kudos
by Anonymous User
Not applicable
Make sure you're not calling IFeature.Store in the event as this will re-fire the behavior. Its called internally once the event is complete.

  public class EditorExtension1 : ESRI.ArcGIS.Desktop.AddIns.Extension   {      private IEditEvents_Event _editEvents;     private int i = 0;      public EditorExtension1()     {     }      protected override void OnStartup()     {       IEditor theEditor = ArcMap.Editor;       _editEvents = theEditor as IEditEvents_Event;       _editEvents.OnChangeFeature += new IEditEvents_OnChangeFeatureEventHandler(_editEvents_OnChangeFeature);     }      void _editEvents_OnChangeFeature(ESRI.ArcGIS.Geodatabase.IObject obj)     {       IFeature feature = obj as IFeature;       feature.set_Value(feature.Fields.FindField("point_x"), (i = i + 1));     }
0 Kudos
EvanBlaisdell
New Contributor III
Boom!  That does it, thank you Sean.  I didn't even think that IFeature.Store() might not be necessary.

Evan.

Make sure you're not calling IFeature.Store in the event as this will re-fire the behavior. Its called internally once the event is complete.

  public class EditorExtension1 : ESRI.ArcGIS.Desktop.AddIns.Extension
  {

    private IEditEvents_Event _editEvents;
    private int i = 0;

    public EditorExtension1()
    {
    }

    protected override void OnStartup()
    {
      IEditor theEditor = ArcMap.Editor;
      _editEvents = theEditor as IEditEvents_Event;
      _editEvents.OnChangeFeature += new IEditEvents_OnChangeFeatureEventHandler(_editEvents_OnChangeFeature);
    }

    void _editEvents_OnChangeFeature(ESRI.ArcGIS.Geodatabase.IObject obj)
    {
      IFeature feature = obj as IFeature;
      feature.set_Value(feature.Fields.FindField("point_x"), (i = i + 1));
    }
0 Kudos
by Anonymous User
Not applicable
No problems and sorry its confusing. Its mentioned in the geodatabase concepts help but i don't think anywhere in editing. I'll see about getting that updated and the individual methods also.
0 Kudos