计时器开始时间和结束时间计算给出了4秒多 [英] Timer start time and end time calculation gives 4 secs extra
本文介绍了计时器开始时间和结束时间计算给出了4秒多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在WP8的工作,我使用计时器我在哪里采掘工作面的总时间
这就是我怎么做的吧:
定时器=新DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1); //定时器间隔为1秒
timer.Tick + =新的EventHandler(timer_Tick); //定时器超时后,该事件被触发
timer.Start();
=的startDateTime DateTime.Now;
日期时间等= DateTime.Now;
的Debug.WriteLine(ST是+ +的startDateTime等是+等);
时间跨度myDateResult =等 - 的startDateTime;
双秒= myDateResult.TotalSeconds;
的Debug.WriteLine(不同的是+秒);
输出,我得到:
ST是2014年3月25日下午3时54分09秒等是2014年3月25日下午3点55分14秒
的区别是64.312636
所以,我1分钟完全运行,那么它给我64秒的输出。为什么这样?这是一个错误?
修改
这里timerCount值60;
私人无效timer_Tick(对象发件人,EventArgs五)
{
timerCount--;
如果(timerCount大于0)
{
TimerText.Text =计时:+ timerCount.ToString();
}
,否则
{
TimerText.Text =定时器;
}
}
解决方案
您正在使用的计算时间错误的工具。你应该秒表,而不是定时器。定时器是用来做一些事情,是指在固定时间间隔发生。
i am working on WP8 , i am using timer where i am calulating the total time.
This is how i am doing it:
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);
Output which i am getting :
st is 3/25/2014 3:54:09 PM et is 3/25/2014 3:55:14 PM
difference is 64.312636
So i run completely for 1 min then its giving me 64 sec as output. why so? is this a bug ?
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 : ";
}
}
解决方案
You are using the wrong tool for calculating time. You should StopWatch instead of timer. Timer is used to do something that is meant to happen at a regular interval.
这篇关于计时器开始时间和结束时间计算给出了4秒多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文