I am working on a very simple stopwatch using WPF but using the System.Diagnostics
stopwatch was very slow and not reliable at all, compared to my system clock every 1 second from my application was 3 seconds on an actual clock.
I did some search about stopwatch being slow, found lots of results but no solution to it, so I decided to come up with my own counter.
Here is an sample of what I came up with:
System.Windows.Threading.DispatcherTimer _update;
DateTime _started;
bool isRunning = false;
The update thread:
_update = new System.Windows.Threading.DispatcherTimer(new TimeSpan(0, 0, 0, 0, 1), System.Windows.Threading.DispatcherPriority.Normal, delegate
{
if (isRunning)
iTimer.Content = new DateTime((DateTime.Now - _started).Ticks).ToString("HH:mm:ss");
}, this.Dispatcher);
I have 2 buttons, bToggle which is resposible for starting, stopping and resuming it and another button called bReset.
private void bReset_Click(object sender, RoutedEventArgs e)
{
isRunning = false;
iTimer.Content = "00:00:00";
bToggle.Content = "Start";
}
private void bToggle_Click(object sender, RoutedEventArgs e)
{
if ((string)bToggle.Content == "Start")
{
isRunning = true;
_started = DateTime.Now;
bToggle.Content = "Stop";
}
else if ((string)bToggle.Content == "Resume")
{
isRunning = true;
bToggle.Content = "Stop";
}
else
{
isRunning = false;
bToggle.Content = "Resume";
}
}
It works fine to start and reset but since I am using the actual time if I stop and resume, it will jump the seconds until the actual time.
How could I solve this problem or is there an alternative to stopwatch that actually have a good accuracy on the current time ?
You need to intruduce an additional variable TimeSpan accumulatedTime
in which you save the elapsed interval whenever someone clicks stop.
And:
iTimer.Content = (new DateTime((DateTime.Now - _started).Ticks) + accumulatedTime).ToString("HH:mm:ss");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With