views:

105

answers:

1

I have a poker blind timer Silverlight app that is loosing time (after running for 1 hour 40 minutes it had lost 3 minutes).

I use a DispatcherTimer timer in my Tournament class and on every tick I raise an event which the UI subscribes to to update the screen (with a DataBound Textblock). I then do checks to see if the blind is over or if there is 60 seconds left etc:

private DispatcherTimer timerBlind;


        if (timerBlind == null)
        {
            timerBlind = new DispatcherTimer();
            timerBlind.Interval = TimeSpan.FromSeconds(1);
            timerBlind.Tick += new EventHandler(Timer_Tick);
        }

  void Timer_Tick(object sender, EventArgs e)
    {
        //check if this would be the end of the blind or other key events
        OnTimerTick(new EventArgs());

        BlindSet.TotalTimeRunning = BlindSet.TotalTimeRunning.Add(TimeSpan.FromSeconds(1));

        if (IsTimerBlindRunning)
        {
            BlindSet.TimeLeftInCurrentBlind = BlindSet.TimeLeftInCurrentBlind.Add(TimeSpan.FromSeconds(-1));

            if (BlindSet.TimeLeftInCurrentBlind.TotalSeconds == 0)
            {
                //advance the level = blinds have gone up
                blindset.EndOfBlindGoToNextLevel();
            }
        }
    }

So, how do I make it more accurate? Thanks for any advice...

+2  A: 

Don't use:

BlindSet.TotalTimeRunning = BlindSet.TotalTimeRunning.Add(TimeSpan.FromSeconds(1));

You are getting cumulative errors because Timers rarely fire exactly on cue.

Instead, store the time from the start of the blind (_startTime) and make a property:

TimeSpan TotalRunningTime{
    get{
        return DateTime.UtcNow-_startTime;
    }
}

Apply this approach to TimeLeftInCurrentBlind too.

spender
Exactly correct. TimeSpans will give you the accurate time math that you need. Also note the excellent use of UTCNow: that's important, DateTime.Now shouldn't be used.
Jeff Wilcox
Thanks guys - that works great.Jeff: With regards to DateTime.Now vs DateTime.UTCNow - I want to show the user the date/time that the tourney started. Am I right in saying that this would be working off the client browser DateTime and therefore I would have to store it in a separate DateTime.Now variable (and not a UTCNow variable). Thanks
Rodney
Ok, I have converted it all to work like your suggestion but for some reason my UI seems to be lagging - the timer skips a second (I'm sure this is just a UI update issue) but I am not sure on how to improve it. I started a new question here due to space restrictions - please take a look and see if you can help:http://stackoverflow.com/questions/3447562/timer-not-updating-regularily-and-skips-a-second-silverlight-4-dispatchertimer
Rodney