Can’t quite figure out how to use ScheduleSolution

Hi,

Sorry to pick up an old conversation. I’m just starting my journey and having the same problem but can’t quite figure out how to use ScheduleSolution(). The script below is where i’m at (mostly hobbled together from pieces of others scripts online as I attempt to figure out what they mean). Any pointers to nudge me in thr right direction would be appreciated.

 private void RunScript(ref object selObjs)
 {
   List<string> objs = new List<string>();
     foreach (RhinoObject obj in RhinoDocument.Objects.GetSelectedObjects(false, false))
       {
         if (obj != null)
           objs.Add(obj.Id.ToString());
       }
   selObjs = objs;

   if (_handler == null)
     _handler = new SelectionEventHandler(Component);
 }

  internal SelectionEventHandler _handler;
  internal class SelectionEventHandler
  {
    private readonly GH_Document _document;
    private readonly IGH_Component _component;

    public SelectionEventHandler(IGH_Component component)
    {
      _component = component;
      _document = component.OnPingDocument();

      Rhino.RhinoDoc.SelectObjects += ObjectsSelected;
      Rhino.RhinoDoc.DeselectObjects += ObjectsDeselected;
      Rhino.RhinoDoc.DeselectAllObjects += AllObjectsDeselected;
     }

     private void ObjectsSelected(object sender, RhinoObjectSelectionEventArgs e)
     {
       TriggerUpdate();
     }
     private void ObjectsDeselected(object sender, RhinoObjectSelectionEventArgs e)
     {
       TriggerUpdate();
     }
     private void AllObjectsDeselected(object sender, RhinoDeselectAllObjectsEventArgs e)
     {
       TriggerUpdate();
     }

     private void TriggerUpdate()
     {
       _component.ExpireSolution(false);
       if (ReferenceEquals(Grasshopper.Instances.ActiveCanvas.Document, _document))
         {
           _document.NewSolution(false);
         }
     }
 }

Hi @jordanmathers.jm,

I’ve moved your topic to the Grasshopper category.

– Dale

If you start scheduling you’ll have to change your expiration logic. The basic steps are this:

  1. Call ScheduleSolution(delay, callback) on the document from whatever code is responsible for determining an update is required. Delay should probably not be much shorter than 5 to 10 milliseconds.
  2. Inside your callback method, expire your component, and nothing else.

The schedule will invoke all registered callbacks and then it will trigger a new solution. If you don’t expire in the callback, your component won’t be part of the new solution, if you expire ahead of time, your component will be in an expired state between solutions. While not the end of the world, it could lead to some awkward behaviour which can be hard to track down.

The only problem here is the signature for the callback method, I think it’s just a void method which takes a single GH_Document.

1 Like

Thanks Dale

Thanks David,

That helped me a lot. Really appreciate it.

For anyone wondering…

private void AllObjectsDeselected(object sender, RhinoDeselectAllObjectsEventArgs e)
{
  _document.ScheduleSolution(10, TriggerUpdate);
}

private void TriggerUpdate(GH_Document gh)
{
  _component.ExpireSolution(false);
}