Grasshopper

algorithmic modeling for Rhino

Ciao to everybody

I'm adding an action on selection of objects with this:  

Rhino.RhinoDoc.SelectObjects += myHandler 

My problem is that everytime that I run the script the myHandler sub is added to the collection of the eventhandlers.

Let me do an example...

Rhino.RhinoDoc.SelectObjects += myHandler 

private void myHandler(object sender, RhinoObjectSelectionEventArgs e)
{
Rhino.RhinoApp.WriteLine("ciao");

}

first time that I run the script the command line shows the word "ciao" each time that I select something

second time the word "ciao" is showed two times. etc

I tried  with this Rhino.RhinoDoc.SelectObjects -= myHandler but it doesn't work.

any idea?

ciao

Vito

Views: 292

Replies to This Discussion

The -= approach ought to work. There's basically two ways to ensure you do not oversubscribe to a certain event. First always unsubscribe before subscribing:

Rhino.RhinoDoc.SelectObjects -= myHandler;

Rhino.RhinoDoc.SelectObjects += myHandler;

It works, but if this code runs a lot you may want to consider a more efficient:

private bool _subscribed = false;

...

if (!_subscribed)

{

  _subscribed = true;

  Rhino.RhinoDoc.SelectObjects += myHandler;

}

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service