我正在开发 WP8,我正在使用计时器来计算总时间。
这就是我正在做的:
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);//interval for timer is 1 sec
timer.Tick += new EventHandler(timer_Tick);//after the timer expires, this event is fired
timer.Start();
startDateTime = DateTime.Now;
DateTime et = DateTime.Now;
Debug.WriteLine("st is "+startDateTime+ " et is "+et);
TimeSpan myDateResult = et - startDateTime;
double seconds = myDateResult.TotalSeconds;
Debug.WriteLine("difference is " + seconds);
我得到的输出:
st is 3/25/2014 3:54:09 PM et is 3/25/2014 3:55:14 PM
difference is 64.312636
所以我完全运行了 1 分钟,然后它给了我 64 秒作为输出。为什么这样?这是一个错误吗?
EDIT
Here timerCount value is 60;
private void timer_Tick(object sender, EventArgs e)
{
timerCount--;
if (timerCount > 0)
{
TimerText.Text = "Timer : "+timerCount.ToString();
}
else
{
TimerText.Text = "Timer : ";
}
}
因此,您使用此计时器计算滴答声,并期望在 60 个 1 秒的滴答声之后,恰好过去了 1 分钟。
它不是那样工作的。应用程序中正在发生其他事情,.NET 永远不会保证它会发生exactly刻度之间一秒。
如果您想要准确性,请使用System.Diagnostics.Stopwatch
它根据系统时钟给出非常准确的经过时间。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)