Hi,
From MSDN:
A Timer is used to raise an event at user-defined interval with an accuracy of 55msec. And the timer is not subject to garbage collection when the value of Timer.Enabled property is true.
Also calling the Timer.Start method is the same as setting Enabled to true. Likewise, calling the Stop method is the same as setting Enabled to false.
So keeping this info in mind I see following issues with your TimerProcess class:
1. You have nowhere set any interval for your timer. So your timer is running with default interval of 100msec.
2. Why are you deriving your TimerProcess from Timer class.
3. In timer_tick why are you calling _timer.Stop(); _timer.Enabled = true ?? These are contradictory. _timer.Stop() will stop the timer, but setting _timer.Enabled to true will start the timer. This looks like the cause for your issue:
"if i click start -> stop button -> start buton , timer will creat new tick event but old event still run. "
4. In the following code, timer_stop() is equivalent to timer_pause(). As you are setting Enabled to true/false in one and calling Start/Stop in other.
public void timer_stop(Button btnStart)
{
_timer.Enabled = false; _label.Text = convertSecond2Hour(TotalTime);
btnStart.Enabled = true;
}
public void timer_pause(Button btnPause)
{
if (btnPause.Text == "Pause")
{
_timer.Stop();
btnPause.Text = "Resume";
}
else if (btnPause.Text == "Resume")
{
_timer.Start();
btnPause.Text = "Pause";
}
}
Actually there is no inbuilt mechanism to pause a timer. You would have to build your own. For ex, you can write your timer_pause function as:
TimeSpan _ts;
public void timer_pause(Button btnPause)
{
if (btnPause.Text == "Pause")
{
_ts= DateTime.Now- _lastTimerStartTime;
_timer.Stop();
btnPause.Text = "Resume";
}
else if (btnPause.Text == "Resume")
{
_timer.Interval=total_Interval- _ts.TotalMilliseconds;
_timer.Start();
btnPause.Text = "Pause";
}
}
And update the _ts, _timer.Interval and _lastTimerStartTime accordingly in timer_start()and timer_stop().
Happy Coding!!
Thanks