How to create custom event that inherits from command class - c#

Hello,

I’ve been struggling to subscribe a form to an event that originates in the rhino command class. I can accomplish this easily when using the existing events but I would like to produce a custom event that alerts me to the completion of a process within my own code.

Questions:

  1. Does a custom class need to inherit from the command class in order for a form to subscribe to it? (ie. CustomRhinoEvent : Rhino.Commands.CommandEventArgs) I understand that this might sound like a silly question but I ask because I have been trying to use higher level event arguments (ie. System.EventArgs) but have not successfully been able to subscribe a form to them when the event originates from a process based in the command class. The opposite (subscribing the command class to a form) does however work.

  2. If a custom class is actually needed to subscribe a form to a command class event, and that class must inherit from the command class eventargs then can someone please provide an example of this custom class?

  3. I could be totally wrong on the above - if so an example for producing a custom event based on System.EventArgs would be appreciated. I have tried but unfortunately have not been able to have a form subscribe to an event in Rhino when using these higher level events. All subscriptions compile successfully but at run time subscriptions are not found by the relevant OnEvent method.

I have been trying to write a custom event class but am receiving no constructor errors or errors that state member names cannot be the same as their enclosing type - each of which are starting to confuse me so examples would be greatly appreciated.

Thank you sincerely.

Can you provide some information on what you are trying to do and why? Perhaps this will allow us to be more helpful.

Tree,

I think the easiest approach for you would to use a Static Helper Class to manage the event(s) you want to create and invoke. Then you can access the event from your form or from within a Rhino Command.

Here is a complete sample that works the way you are hoping:

Create a static class like this:

public static class EventHelper
{
    public static event EventHandler SphereCreated;

    public static void OnSphereCreated()
    {
        SphereCreated?.Invoke(null, EventArgs.Empty);
    }
}

Next add the handler to the OnSphereCreated event inside your form like this:

EventHelper.SphereCreated += EventHelper_SphereCreated;

Then you simply invoke the event from within your command or elsewhere like this:

EventHelper.OnSphereCreated();

jStevenson!

Awesome - it works!

After two days of trying to find a solution, thank you very much for your help!

1 Like

Dale,

Thanks for the note. JStevenson’s answer worked.

Regarding what I’m trying to do - I have a slew of processes that get triggered from a form. I need to wait for those to complete before the form can continue doing its work so the custom event seemed useful. Hope this helps.