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?

ChangeSliderManually.gh (4.6 KB)

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.

Hi @DavidRutten,

I implement a genuine Timer class for my c# component in visual studio environment.
But the grasshopper completely froze when I trying to reload the script.
Do you have any example for it?
I’m also developing a c# component with the need to increase a slider value every interval, but it seems like it’s not that simple, and the timer component in grasshopper also isn’t doing the job.

Regards,
Shaun

I have also tried the sleep method with a for loop.

“System.Threading.Thread.Sleep(5000);”

But it doesn’t dynamically increase the value, it just wait the total sleep duration, and show the result in the very end.

Don’t use Thread.Sleep(), it just suspends the current thread for a long time and then picks up where it left of. Nothing else is allowed to happen on that thread in the meantime. It’s a handy method for debugging, but not much else.

TimerInCSharp.gh (12.9 KB)

But I reiterate that GH_Document.ScheduleSolution() would be preferable to spinning your own timers.

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.

I have previously successfully used ScheduleSolution() to handle websocket messages containing a single set of slider values. I used the method you provided here I have also looked at Colibri, but I specifically need to apply sets of slider values at a time. This is to save you time, I’ve done my research.

I am trying to get Grasshopper iterate through a list of “sets” of slider values from within my component. I am basically getting grasshopper to batch execute sets of generated slider values that will be read by my plugin when loaded in a grasshopper definition. What is the best way to do this using ScheduleSolution() ? I would like it to start executing the moment the definition loads, and not from a UI event. Can you please provide a code sample for the Class instantiation method you described ?

Thanks

PS- In Rhino6, I am able to change slider values, but the Geometry dependent on these sliders is not recomputing (right now it’s just a cube). The C# script you provided doesn’t work either.

To recap -
This is the code you provided in 2017 but doesn’t work for Rhino6 as is. I use the same logic in my component.
This is my modification
and partially works, but behaves unpredictably. The number of times it loops over the sliders is out of control.

Please let me know if you have a solution.
Thanks