Event Handler Bug for selection events

Hello fellow grasshoppers,

A little context, I’m currently developing a user interface that relies heavily on the selection of rhino geometry to display custom information in the Rhino canvas.

And, it is working well, but my current issue goes beyond this usage.

I can’t delete objects or manipulate them in any way efficiently, they seem to start to flicker, even though I believe I disable redraw during the watching of the event.

I’m not the best programmer, and have grown a lot though this forum, so feel free to correct me where I’m wrong, all input is well recieved :slight_smile: .

Here is a small video of what I’m trying to describe:

Here are the Rhino and Grasshopper files if you want to use check them out. (the code was made from parts of other posts from this great forum, I’ll try to find them to give the proper shoutout):
EventHandlerBug.3dm (55.5 KB)
EventHandlerBug.gh (6.5 KB)

Here is the code if anyone needs it:

private void RunScript(ref object GUIDS)
{
if (!_init)
{
// Keeps the layer panel from flickering…
this.RhinoDocument.Views.EnableRedraw(false, false, false);

  //Subscribe and Unsibscribe to events
  Rhino.RhinoDoc.SelectObjects -= RhinoDocOnSelectObjects;
  Rhino.RhinoDoc.SelectObjects += RhinoDocOnSelectObjects;
  Rhino.RhinoDoc.DeselectObjects -= RhinoDocOnSelectObjects;
  Rhino.RhinoDoc.DeselectObjects += RhinoDocOnSelectObjects;
  Rhino.RhinoDoc.DeselectAllObjects -= RhinoDocOnDeselectAllObjects;
  Rhino.RhinoDoc.DeselectAllObjects += RhinoDocOnDeselectAllObjects;

  _init = true;
}

//Output Data
GUIDS = guids;

//Re-Enables Draw in Rhino
this.RhinoDocument.Views.EnableRedraw(true, true, true);

}

//

//Function
private bool _init = false;
List guids = new List();

//Function to subscribe to selection events and retrive selection guids
private void RhinoDocOnSelectObjects(object sender, RhinoObjectSelectionEventArgs e)
{
foreach (var rhObj in RhinoDoc.ActiveDoc.Objects.GetSelectedObjects(true, false))

  if (!guids.Contains(rhObj.Id))
    guids.Add(rhObj.Id);

Component.ExpireSolution(true);

}

//Function to subscribe to deselection events
private void RhinoDocOnDeselectAllObjects(object sender, RhinoDeselectAllObjectsEventArgs e)
{
guids.Clear();
Component.ExpireSolution(true);
}

Also, if I delete the component where the events are handled, the problem doesn’t go away!

I’m not sure why you are adding and then immediately removing the event handling. Typically you would want to add an event handler (say when the component is created) and then remove the event handler when the component is deleted (for example). There’s actually quite a lot of edge cases that you have to be aware of depending on what you’re trying to do, but I don’t think you’ve got your adding and removing of handlers set up correctly.

Thanks for you input Andy. So, I’m not an expert on events, I just started using them like two weeks ago, so I may lack experience in how to use them, but that was how it was advised to be used in this post:
Python - Get Selected On Selection Change Event Handler - Grasshopper Developer - McNeel Forum

Anyways, I don’t really understand how this would help solve the core issue, because even if I don’t unsubscribe to the events, the main problem keeps happening! How would you suggest working around the selection events being triggered every time I delete an object?