Silverlight定时器问题
我正在开发一个带有自定义动画的 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。我不知道为什么。我有什么遗漏的吗?
谢谢
I am developing a Silverlight application with custom animations. I want to update the variable animationCounter every 1 milissecond, so that in one second the value is 1000. I've tried DispatcherTimer and System.Threading.Timer. this way:
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());
}
with 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());
}
Both of them are setting AnimationCounter around 100 in one second. Should be 1000. I don't know why. Is there anything I'm missing.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文档应说明计时器的分辨率不是 1 毫秒,而是最小 10 毫秒;)似乎没有。无论如何,最小计时器分辨率约为 10 毫秒……所以这是它们触发的最小间隔。
到底为什么(抱歉)你需要 1 毫秒?对我来说听起来没什么用。每秒更新 25 - 60 次左右的动画应该没问题 - 其余的无论如何眼睛都看不到。
Documentation should state that the timers do not have a resolution of 1ms, but of 10ms minimum ;) It does no tseem to. Anyhow, minimal timer resolution is around 10ms... so that is the smallest interval they fire.
Why the heck (sorry) do you need 1ms anyway? Sounds useless to me. An animation should be ok with around 25 - 60 updates per second - the rest the eye can not see anyway.