Create a simple timer in Grasshopper using the built-in C# component with this example script.
What this does
Set runtimer to true. The component will build a list of the current time and date, once per second.
When runtimer is set to false, the timer is turned off and the list is displayed.
Code
private void RunScript(bool runtimer, bool reset, ref object A) { if(runtimer) Example.TurnOnTimer(); if(!runtimer) Example.TurnOffTimer(); A = rtnlist; } // <Custom additional code> public static List<string> rtnlist = new List<string>(); public class Example { public static System.Timers.Timer aTimer = new System.Timers.Timer(1000); public static void TurnOnTimer() { rtnlist.Clear(); aTimer.Elapsed += OnTimedEvent; aTimer.Enabled = true; } public static void TurnOffTimer() { aTimer.Elapsed -= OnTimedEvent; aTimer.Enabled = false; } private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e) { rtnlist.Add(e.SignalTime.ToString()); } }
Download
You can download the above code as a Grasshopper user object here
Grasshopper simple timer in C#
1 file(s) 2.06 KB
Further information
Grasshopper timer using Python
If you are looking to add a timer to a Python component without using the timer component, have a look at Anders Deleuran’s code here.