从毫秒开始,计时器与 TimeSpan 无法正常工作 [英] timer is not working well with TimeSpan from milliseconds
问题描述
我正在开发适用于 Windows Phone 8 的应用我创建了一个 DispatcherTimer
I am working on my app for Windows Phone 8 and i created a DispatcherTimer
DispatcherTimer Timer= new DispatcherTimer();
private int TimePass;
public TapFast()
{
TimePass = 0;
Timer.Tick += new EventHandler(TimePassTick);
Timer.Interval = TimeSpan.FromMilliseconds(1);
InitializeComponent();
}
public void TimePassTick(Object sender, EventArgs args)
{
TimePass++;
TimeSpan t = TimeSpan.FromMilliseconds(TimePass);
string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D4}ms",
t.Hours,
t.Minutes,
t.Seconds,
t.Milliseconds);
time.Text = answer;
}
当我启动计时器时,我在 TextBlock 中没有得到正确的结果
When i start the timer , i don't get the right result in the TextBlock
1 秒 = 1000 毫秒
1 second = 1000 milliseconds
我必须看到 0 的值在 1 秒内增加到 1000但达到 1000 毫秒需要 18-20 秒
I must see a value of 0 increases to 1000 in 1 second but it takes like 18-20 seconds to get to 1000ms
有什么问题?
推荐答案
Windows 和关联的 API 不是实时的".您的计时器间隔太小,Windows 无法按您要求的时间表为其提供服务.
Windows and the associated APIs are not "real time". Your timer interval is so small that there's no way for Windows to service it on the schedule you're asking for.
对于非实时操作系统和 API,您不能假设计时器实际上是在您要求的时间间隔内发出信号的.您需要自己跟踪实际时间(例如使用 System.Diagnostics.Stopwatch
类)并适应您遇到的变化.
With non-real-time OS and APIs, you can't assume that the timer is actually signaled on exactly the interval you asked for. You need to keep track of the actual time yourself (e.g. using the System.Diagnostics.Stopwatch
class) and accommodate variations you experience.
这篇关于从毫秒开始,计时器与 TimeSpan 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!