代码之家  ›  专栏  ›  技术社区  ›  Rodney

Silverlight 4中的调度员正在浪费时间-什么更准确?

  •  0
  • Rodney  · 技术社区  · 14 年前

    我有一个 poker blind timer

    我在我的锦标赛类中使用了一个dispatcher计时器,每次我都会引发一个事件,UI订阅该事件来更新屏幕(带有一个数据绑定的Textblock)。然后我做检查,看看盲人是否已经结束,或者是否还有60秒等:

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

    1 回复  |  直到 12 年前
        1
  •  3
  •   spender    14 年前

    不要使用:

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

    你得到的是累积错误,因为计时器很少完全按提示开火。

    _startTime )并创建一个属性:

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

    将此方法应用于 TimeLeftInCurrentBlind