Stop Rhino.Runtime.HostUtils?

Hi
How to stop Rhino.Runtime.HostUtils from sending data?

static void CurrentData()
{
    string data;
    using (var args = new Rhino.Runtime.NamedParametersEventArgs())
    {
        args.Set("new", data);
        Rhino.Runtime.HostUtils.ExecuteNamedCallback("NewData", args);
    }
}

What are you trying to do?

Hi @stevebaer

I found a way where bool start changes depending on event

static void CurrentData()
{
    if(start)
    {
        string data;
        using (var args = new Rhino.Runtime.NamedParametersEventArgs())
        {
            args.Set("new", data);
            Rhino.Runtime.HostUtils.ExecuteNamedCallback("NewData", args);
        }
    }
}

I having having the hardest time trying to understand both what you are doing and what you are trying to achieve. ExecuteNamedCallback is a pretty obscure part of RhinoCommon and not typically used by plugin developers.

I have other topics related to this about Rhino plugin and RhinoInside plugin.
I used ExecuteNamedCallback in Rhino plugin to send some data like active view and mouse point …etc.

Can I politely ask you to write something off-topic again? :smiley:

It sound that you are mixing things here! A .Net event is basically a callback mechanism with syntactical sugar and some extra features. But it’s a callback mechanism. And a callback is nothing else as a function passed as argument into another function, being executed at some point in that function. To pass a function, you need to pass its function pointer, called “delegate”. In C# you either define your own delegate (What Rhino does) or you use some generic ones (“Action”=no return type and “Func”=returns something).

void SomeMethod(Func<bool> callback)
{
     bool result = callback() // here we execute the provided function
}

What does this mean? If you are not the owner of SomeMethod, meaning you are not in charge to call or to modify this method, you have no chance to determine when the given callback is executed or not. An event mechanism allows you to only subscribe to a given event, allowing you to inject your own callbacks. But you shouldn’t be allowed to control when it fires from “outside”. Having the ability would be an antipattern, and it would defeat the purpose of the so-called “observer” pattern.

The only thing you can do is to replace SomeMethod during Runtime, which is an advanced and dangerous hack! But since events allow you to have more “subscribers”, it is really a bad idea to mess around with when something is firing. Because suddenly another system doesn’t receive expected feedback anymore, breaking the logic in various other places.

A very simple implementation of an event system (without using the C# inbuild language features) could look like this:

        /// <summary>
        /// Application owner wrote it...
        /// </summary>
        public class SimpleEventMechanism
        {
            private List<Action> _callbacks = new List<Action>();

            public void Subscribe(Action callback) => _callbacks.Add(callback);

            private void Fire()
            {
                if (_callbacks != null)
                {
                    foreach (var callback in _callbacks)
                    {
                        callback();
                    }
                }
            }    
            public void SomePracticalMethod()
            {
                // ...
                Fire();
                // ...
            }        
        }
        /// <summary>
        /// Could come from a plugin developer
        /// </summary>
        public class BasicSubscriber
        {
            public BasicSubscriber(SimpleEventMechanism sem)
            {
                sem.Subscribe(() => Console.WriteLine("That callback was fired"));
                sem.Subscribe(() => Console.WriteLine("This callback as well"));
            }
        }

        static void RunSomeonesAppCode()
        {
            var sem = new SimpleEventMechanism();
            var subscriber = new BasicSubscriber(sem);

            sem.SomePracticalMethod();

        }

I hope this explains why you are trying something almost impossible here…