2010-08-10 23 views
2

我希望得到一些幫助,下面的問題 - 你可以看到我的live poker blinds timer這個問題:計時器不更新regularily和「跳過」第二(Silverlight 4中DispatcherTimer)

主時鐘(盲定時器倒計時)開始在20:00關閉,然後跳到19:58。水平計時器(在屏幕頂部計數) - 同步開始,因此是第二次。

這裏是我的代碼:XAML:

TextBlock Text="{Binding TimeLeftInCurrentBlindFormatted}" 

和我的比賽類:

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"); 
       } 
      } 
     } 

當計時器通過UI開始日期時間設置:

TimeStarted = DateTime.UtcNow; 

我認爲這是事實上,Tick並不一定完全是秒,UI是滯後的以某種方式跳過秒,但兩個計時器同時在Tick事件中更新(TimeRunningForCurrentBlindFormatted(這是最早用過的時間)和TimeLeftInCurrentBlindFormatted)。

在我的開發系統上,定時器從20點到19點59分,然後到19點57分。

回答

2

不要每秒激活DispatcherTimer。更頻繁地開火...也許每100毫秒。

+0

*歎爲觀止 - 感謝您的幫助! – Rodney 2010-08-11 07:50:17

1

從這裏,DispatcherTimer Class

定時器不能保證什麼時候的時間間隔發生時立即執行 , 但他們都保證時間間隔發生之前不會執行 。此 是因爲DispatcherTimer操作 與其他操作一樣放在Dispatcher隊列 上。執行 DispatcherTimer操作時,執行的操作是 ,具體取決於隊列中 隊列中的其他作業及其優先級。