Timer tick event count
-
Hi all, I've been trying to solve this problem for over an hour and as I'm a rookie I know that what I want to do is easy to implement, but my lack of experience is preventing me from doing so. With that said, I have some code for a ZedGraph dynamically updating control. Notice that the variable double time controls the x Scale. This code is executed in a timer tick event in order to draw the line on the graph. However, as the x-scale is driven by Environment.Tickcount (which is totally independent of the timer) the graph x-axis increments regardless of whether the timer is enabled or not. I want the double time to be replaced with a double that is based upon the elapsed time of the timer and not the Environment.TickCount, but I can't figure this out. Maybe because I'm too tired... :) Thanks in advance for any help.
//make sure curvelist has one curve
if (zedGraphControl1.GraphPane.CurveList.Count <= 0)
return;
//Get first CurveItem in Graph
LineItem curve = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
if (curve == null)
return;
// Get the PointPairList
IPointListEdit list = curve.Points as IPointListEdit;
// If this is null, it means the reference at curve.Points does not
// support IPointListEdit, so we won't be able to modify it
if (list == null)
return;// Time is measured in seconds double time = (Environment.TickCount - tickStart) / 1000.0; // 3 seconds per cycle list.Add(time, dblLabel); // Keep the X scale at a rolling 30 second interval, with one // major step between the max X value and the end of the axis Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale; if (time > xScale.Max - xScale.MajorStep) { xScale.Max = time + xScale.MajorStep; xScale.Min = xScale.Max - 30.0; } // Make sure the Y axis is rescaled to accommodate actual data zedGraphControl1.AxisChange(); // Force a redraw zedGraphControl1.Invalidate();
-
Hi all, I've been trying to solve this problem for over an hour and as I'm a rookie I know that what I want to do is easy to implement, but my lack of experience is preventing me from doing so. With that said, I have some code for a ZedGraph dynamically updating control. Notice that the variable double time controls the x Scale. This code is executed in a timer tick event in order to draw the line on the graph. However, as the x-scale is driven by Environment.Tickcount (which is totally independent of the timer) the graph x-axis increments regardless of whether the timer is enabled or not. I want the double time to be replaced with a double that is based upon the elapsed time of the timer and not the Environment.TickCount, but I can't figure this out. Maybe because I'm too tired... :) Thanks in advance for any help.
//make sure curvelist has one curve
if (zedGraphControl1.GraphPane.CurveList.Count <= 0)
return;
//Get first CurveItem in Graph
LineItem curve = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
if (curve == null)
return;
// Get the PointPairList
IPointListEdit list = curve.Points as IPointListEdit;
// If this is null, it means the reference at curve.Points does not
// support IPointListEdit, so we won't be able to modify it
if (list == null)
return;// Time is measured in seconds double time = (Environment.TickCount - tickStart) / 1000.0; // 3 seconds per cycle list.Add(time, dblLabel); // Keep the X scale at a rolling 30 second interval, with one // major step between the max X value and the end of the axis Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale; if (time > xScale.Max - xScale.MajorStep) { xScale.Max = time + xScale.MajorStep; xScale.Min = xScale.Max - 30.0; } // Make sure the Y axis is rescaled to accommodate actual data zedGraphControl1.AxisChange(); // Force a redraw zedGraphControl1.Invalidate();
Just set up:
private double myTime = 0.0;
as a class field. In your Load event or constructor:
Timer tim = new Timer();
tim.Interval = 1000;
tim.Tick += new new EventHandler(timer_Tick);
tim.Start();And add the Tick event handler:
void timer_Tick(object sender, EventArgs e)
{
myTime+= 1.0;
}However, it may be easier and better to save the start time as a DateTime; and subtract it from DateTime.Now in your update method. You can then get the elapsed seconds from the resulting TimeSpan.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
Just set up:
private double myTime = 0.0;
as a class field. In your Load event or constructor:
Timer tim = new Timer();
tim.Interval = 1000;
tim.Tick += new new EventHandler(timer_Tick);
tim.Start();And add the Tick event handler:
void timer_Tick(object sender, EventArgs e)
{
myTime+= 1.0;
}However, it may be easier and better to save the start time as a DateTime; and subtract it from DateTime.Now in your update method. You can then get the elapsed seconds from the resulting TimeSpan.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
You're welcome!
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy