我正在开发一个带有自定义动画的 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。我不知道为什么。有什么我想念的吗。
谢谢