Creating a C# component to cycle through slider values



I am trying to create a c# component to cycle through different slider values (in a similar manner to galapagos.)
In my example I have tried to change the slider value based on a timer.

It appears to be working - however the grasshopper document crashes most times or temporarily goes into a not responding state before recovering.

What can I do to prevent this? (4.6 KB)

(David Rutten) #2

You cannot change slider values within solutions. Because this will expire the slider and all objects that depend on the slider causing a potentially infinite recursion of objects expiring other objects. Or rather, it will recurse until the call stack is full at which point you will crash.

If you want to do something 5 seconds from now, there’s two sensible approaches:

  1. Use a genuine Timer class and handle the callback. Note that these timers run on threads other than the UI thread, and thus the callback will be invoked on a thread other than the UI thread. This means that before you tell Grasshopper to do anything like start a new solution, you must first make sure that your code is hoisted over to the correct thread. This can be done using the Invoke on any winforms control, or RhinoApp.InvokeOnUiThread.
  2. Or better yet, use the GH_Document.ScheduleSolution method which does all the above behind the scenes. You provide a callback when you schedule a solution and inside that callback you change the slider values.

Alternatively you can write your code in a class which runs outside of the solution (like Galapagos). This can mean putting it inside a window and have it triggered by button presses or other UI events, or it can mean creating a class instance which gets ‘activated’ once a solution is finished.