2

我正在开发一个带有自定义动画的 Silverlight 应用程序。我想每 1 毫秒更新一次变量 animationCounter,以便在一秒钟内该值为 1000。我尝试过 DispatcherTimer 和 System.Threading.Timer。这边走:

DispatcherTimer timer = new DispatcherTimer(); (...)
timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
timer.Tick += new EventHandler(timer_Tick); (...)

(...)

void timer_Tick(object sender, EventArgs e)
{
      animationCounter++;
      Dispatcher.BeginInvoke(() => txtAnimationCounter.Text = animationCounter.ToString());
}

使用 System.Threading.Timer

System.Threading timer = null;
timer = new System.Threading.Timer(UpdateAnimationCounter, 0, 1);

void UpdateAnimationCounter(object state)
{
                animationCounter++;
      Dispatcher.BeginInvoke(() => txtAnimationCounter.Text = animationCounter.ToString());
}

他们都在一秒钟内将 AnimationCounter 设置为 100 左右。应该是1000。我不知道为什么。有什么我想念的吗。

谢谢

4

1 回答 1

3

文档应说明计时器的分辨率不是 1 毫秒,而是最小 10 毫秒;)它似乎没有。无论如何,最小的计时器分辨率大约是 10 毫秒……所以这是它们触发的最小间隔。

为什么(对不起)你还需要 1 毫秒?对我来说听起来没用。每秒更新大约 25 - 60 次的动画应该没问题 - 其余的眼睛无论如何都看不到。

于 2010-03-09T11:33:45.640 回答