views:

88

answers:

2

I would appreciate some help with the following issue - you can see this problem on my live poker blinds timer:

The main clock (Blind timer countdown) starts off on 20:00 and then jumps to 19:58. The Level timer (which counts up at top of screen) - starts off in synch and is therefore a second out.

Here is my code: XAML:

TextBlock Text="{Binding TimeLeftInCurrentBlindFormatted}"

and my Tournament class:

    private DispatcherTimer timerBlind;
    private DateTime? blindTimeStarted = null;

    public DateTime? BlindTimeStarted
    {
            get
            {
                return blindTimeStarted;
            }
            set
            {
                if (blindTimeStarted != value)
                {
                    blindTimeStarted = value;
                    OnPropertyChanged("BlindTimeStarted");
                    OnPropertyChanged("TimeLeftInCurrentBlind");
                    OnPropertyChanged("TimeLeftInCurrentBlindFormatted");
                    OnPropertyChanged("TimeRunningForCurrentBlind");
                    OnPropertyChanged("TimeRunningForCurrentBlindFormatted");
                }
            }
        }


            public TimeSpan TimeLeftInCurrentBlind
            {
                get
                {
                     return BlindTimeStarted == null ? blindset.CurrentBlind.BlindDuration : BlindTimeStarted.Value.Add(blindset.CurrentBlind.BlindDuration).Subtract(DateTime.UtcNow.Subtract(TotalTimePausedForCurrentBlind)); 
                }
            }
            public string TimeLeftInCurrentBlindFormatted
            {
                get { return Utils.FormatTime(TimeLeftInCurrentBlind); }
            }



            void Timer_Tick(object sender, EventArgs e)
            {
                if (IsTimerBlindRunning)
                {
                    OnPropertyChanged("TimeRunningForCurrentBlindFormatted");
                    OnPropertyChanged("TimeLeftInCurrentBlindFormatted");
                }
            }
        }

When the timer is started through the UI the datetime is set:

TimeStarted = DateTime.UtcNow;

I assume it is something to do with the fact that the Tick is not neccessarily exactly a second and the UI is lagging somehow and skipping a second, but both timers are updated in the Tick event at the same time (TimeRunningForCurrentBlindFormatted (which is the top Elapsed time) and the TimeLeftInCurrentBlindFormatted).

On my dev system the timer goes from 20:00 to 19:59 and then to 19:57.

+1  A: 

From here, DispatcherTimer Class:

Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. This is because DispatcherTimer operations are placed on the Dispatcher queue like other operations. When the DispatcherTimer operation executes is dependent on the other jobs in the queue and their priorities.

Mitch Wheat
+1  A: 

Don't fire the DispatcherTimer every second. Fire it more frequently... perhaps every 100ms.

spender
*sigh* that makes perfect sense - thanks for your help!
Rodney