Hey guys,
Ive been messing around in visual studio trying to figure out how to do time-based simulation without using the timer component within grasshopper. I created a simple counter that should add one every second, but instead, it seems to spit out the result*result. (e.g. 0, 1, 2, 4, 8, 16 etc) until it eventually crashes. wondering if anyone knows what I’m doing wrong?
Below the relevant sections:
public class TimerTestComponent : GH_Component
{
Timer timer = new Timer();
double n = 0;
bool reset, run;
public TimerTestComponent()
: base("TimerTest", "TimeT", "Used to test time-based components", "User", "Debug")
{
timer.Interval = 1000;
timer.Start();
}
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddBooleanParameter("Run", "RUN", ".", GH_ParamAccess.item);
pManager.AddBooleanParameter("Reset", "RES", ".", GH_ParamAccess.item);
pManager.AddNumberParameter("Number", "N", "number to be affected.", GH_ParamAccess.item);
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddNumberParameter("Result", "R", "Result.", GH_ParamAccess.item);
}
protected override void SolveInstance(IGH_DataAccess DA)
{
if (!DA.GetData(0, ref run)) return;
if (!DA.GetData(1, ref reset)) return;
if (reset) if (!DA.GetData(2, ref n)) { return; }
if (run)
{
Update();
n++;
}
DA.SetData(0, n);
}
public void Update()
{
timer.Elapsed += new ElapsedEventHandler(UpdateSolution);
}
public void UpdateSolution(object source, ElapsedEventArgs e)
{
ExpireSolution(true);
}
}
}