Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to stopwatch?

Tags:

c#

wpf

stopwatch

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 ?

like image 321
Guapo Avatar asked May 04 '12 10:05

Guapo


1 Answers

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");
like image 118
usr Avatar answered Sep 20 '22 22:09

usr