internetmw internetmw - 1 month ago 28
C# Question

How do I reset a dispatcherTimer?

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 I set the timer to reset and start at zero?

Answer

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(); 
Comments