RhinoCommon Event handler issue

Hi,
I am facing an issue with event handlers. Let’s say there are 2 Rhino plugins, plugin A and plugin B. Both have an event handler for Rhino.Commands.Command.BeginCommand. When I run “BlockEdit” command, plugin A crashes due to a bug, and when this happens, the plugin B doesnt handle the event anymore.
I am working on plugin B, and the issue I am having is that when some other plugin is crashing when handling the event, my code doesnt get executed. And sometimes I am not sure which plugin is crashing when handling events. Is this a bug? Is there a work around for this?

Lol, well yes - in the plug-in that is crashing. I don’t think there is anything we can do about this…

– Dale

Yes definitlely. This time I could find that out, as both the plugins were written by me. But I was wondering how to track this if at all it is a third party plugin that is crashing. There is generally no warning when this happens.

Yep, this is my experience as well. When working with events it is either working or it crashes Rhino.

Hi @Darryl_Menezes,

We always advise developers to no do too much in event handlers, especially document modifications. It’s better to set a flag and then handle the event later in a Rhino.Idle handler.

Here is a pattern I use frequently:

/// <summary>
/// Event handlers
/// </summary>
private EventHandler<DocumentEventArgs> m_new_document;
private EventHandler<DocumentOpenEventArgs> m_end_open_document;
private EventHandler m_idle;

/// <summary>
/// Event type
/// </summary>
enum EventType
{
  None,
  NewDocument,
  EndOpenDocument,
}
private EventType m_event_type;

/// <summary>
/// Hook Rhino events
/// </summary>
private void HookRhinoEvents()
{
  if (null == m_new_document)
    RhinoDoc.NewDocument += m_new_document = OnNewDocument;

  if (null == m_end_open_document)
    RhinoDoc.EndOpenDocument += m_end_open_document = OnEndNewDocument;
}

/// <summary>
/// Unhook Rhino events
/// </summary>
private void UnhookRhinoEvents()
{
  if (null != m_new_document)
  {
    RhinoDoc.NewDocument -= m_new_document;
    m_new_document = null;
  }

  if (null != m_end_open_document)
  {
    RhinoDoc.EndOpenDocument -= m_end_open_document;
    m_end_open_document = null;
  }
}

/// <summary>
/// Hook Rhino.Idle event
/// </summary>
private void HookRhinoIdle(EventType eventType)
{
  if (null == m_idle)
  {
    m_event_type = eventType;
    RhinoApp.Idle += m_idle = OnIdle;
  }
}

/// <summary>
/// Unhook Rhino.Idle event
/// </summary>
private void UnhookRhinoIdle()
{
  m_event_type = EventType.None;
  if (null != m_idle)
  {
    RhinoApp.Idle -= m_idle;
    m_idle = null;
  }
}

/// <summary>
/// RhinoDoc.NewDocument event handler
/// </summary>
private void OnNewDocument(object sender, DocumentEventArgs args)
{
  HookRhinoIdle(EventType.NewDocument);
}

/// <summary>
/// RhinoDoc.EndNewDocument event handler
/// </summary>
private void OnEndNewDocument(object sender, DocumentOpenEventArgs args)
{
  HookRhinoIdle(EventType.EndOpenDocument);
}

/// <summary>
/// RhinoApp.Idle event handler
/// </summary>
private void OnIdle(object sender, EventArgs e)
{
  switch (m_event_type)
  {
    case EventType.NewDocument:
      {
        // todo
      }
      break;
    case EventType.EndOpenDocument:
      {
        // todo
      }
      break;
  }
  UnhookRhinoIdle();
}

Hope this helps,

– Dale

1 Like

Hi @dale,

Suppose you’re executing a script, will OnIdle be triggered during the execution of the script?

It can be if you add the event handler to the sticky settings.

SampleEventHandler.py

– Dale

Yes, I use a modified version of this, still if you do something as little as having a typo, result will be crashing Rhino.