windows phone DispatcherTimer 精度 [英] windows phone DispatcherTimer precision

查看:27
本文介绍了windows phone DispatcherTimer 精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我得到了一个使用 DispatcherTimer 来模拟计时器的应用程序(windows phone 7.5 over c#).我已将间隔设置为 1 毫秒:

So, i got an application (windows phone 7.5 over c#) using an DispatcherTimer to emulate a timer. I have set the interval to 1 millisecond:

timer.Interval = new TimeSpan(0,0,0,0,1);

然后我声明一个 TimeSpan 以便进行简单的倒计时:

Then i am declaring a TimeSpan in order to make a simple countdown:

TimeSpan countdown = TimeSpan.FromSeconds(10);

在每个刻度上减去 1 毫秒:

On each tick im Substracting 1 millisencond:

countdown = countdown.Subtract(TimeSpan.FromMilliseconds(1));

在 Visual Studio 的 windows 手机模拟器 256/512 中,它的工作没有问题",但是当我在我的手机 (LG Quantum) 上运行它时,它似乎有精度问题.我将它与运行该应用程序的 PC 屏幕进行了比较,并且在手机上运行速度较慢.我试图不在我的手机中以调试模式运行该应用程序.有什么解决办法吗?或者我必须采取其他方法?

In Visual Studio's windows phone emulator 256/512 its working with 'no problems', but when i run it on my phone (LG Quantum) it seems to have precision problems. I have compared it with my PC screen running the application and its way slower on the phone. I have tried to not to run the application in debug mode in my phone. Is there any solution to this? Or i have to take another approach?

推荐答案

经验告诉我,您只是想触发太多事件让手机硬件无法处理.

Experience tells me that you're simply trying to fire far too many events for the phone hardware to handle.

来自 MSDN:

在每个 DispatcherTimer 循环的顶部重新评估 DispatcherTimer.

The DispatcherTimer is reevaluated at the top of every DispatcherTimer loop.

不保证定时器在时间间隔发生时准确执行,但保证在时间间隔发生前不执行.

Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs.

请记住,实际的手机硬件是单核的,因此试图每秒精确捕获 1000 个事件并不是最好的方法.

Remember that the actual phone hardware is single core so trying to capture precisely 1000 events per second isn't the best approach.

要么增加滴答之间的时间并根据新的间隔递增,要么将计时器启动的时间从每个滴答的当前时间中减去.

Either increase the time between ticks and increment based on the new interval, or take the time the timer is started and subtract it from the current time each tick.

这篇关于windows phone DispatcherTimer 精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆