Timer not updating regularily and “skips” a second (Silverlight 4 DispatcherTimer)

本秂侑毒 提交于 2019-12-11 02:37:59

问题


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:


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




回答2:


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.



来源:https://stackoverflow.com/questions/3447562/timer-not-updating-regularily-and-skips-a-second-silverlight-4-dispatchertime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!