How do I reset a dispatcherTimer?
in my app I start a timer and later on I use the dispatcherTimer.Stop method to stop and the timer stops. I close the application and run it again and the timer doesn't start at zero. How do 开发者_JS百科I set the timer to reset and start at zero?
It looks like what you are calling 'timer count' is:
string etime = DateTime.Now.Second.ToString();
Since DateTime.Now is based on the computers realtime clock, you are going to get a different value for seconds based on when you start the program.
You want:
starttime = DateTime.Now;
dispatcherTimer.Start();
and:
string etime = (DateTime.Now - startTime).Seconds.ToString();
this will reset a dispatchertimer.
timer.Stop();
timer.Interval = new TimeSpan(0, 0, 0, 1); // 1sec
timer.Start();
Timers count down. This is true for all software timers I know of. You give it a specified time, and it counts down, triggering an event when it reaches zero.
If you want to measure elapsed time, then you can use something like a StopWatch
, which does count up.
精彩评论