AddRhinoObject raised twice when event handler references class param


#1

Apologies if this is a basic programming question. Typically when moving an object in Rhino, the AddRhinoObject event is raised once. However, if I reference a class field inside the event handler, the event is raised twice (see below). How can I prevent this from happening?

    internal class RhinoListenerClass
    {
        private Task listener;
        private List<double> data;

        internal RhinoListenerClass()
        {
            data = new List<double>();
            listener = RhinoListener();
        }

        private async Task RhinoListener()
        {
            await Task.Run(() =>
            {
                RhinoDoc.AddRhinoObject += (s, e) =>
                {
                    Eto.Forms.MessageBox.Show("AddRhinoObject");
                    var d = data; // this causes the event to be raised twice
                };
            });
        }
    }

Thanks :slight_smile:

Jon


(Dale Fugier) #2

Hi @Jon,

Just curious, why are you using async? Rhino is not mutli-threaded (you probably know this).

Have you tried the SampleCsEventWatcher plug-in? Does is also produce the same double event?

– Dale


#3

Hi @dale,

Thanks for the example! Changing my class to a singleton does the trick. :slight_smile:

Async wasn’t the culprit in this case but I take your point, I’ll remove it.

Jon