问题描述
我使用计时器组件制作倒数计时器,以剩余剩余时间更新标签。问题是即使间隔设置为1,标签也不会每毫秒更新一次。我在不同的网站上看过那是因为它不是一个真实的环境。
我还制作了一个使用DateTime来计算时间差的计时器使用与倒数计时器相同的计时器组件启动计时器和实际时间。这确实每毫秒更新一次标签。
看到DateTime方法确实每隔毫秒更新标签我想知道,是否可以使用DateTime制作倒数计时器从a减少到设定的时间(小时,分钟,秒,毫秒)的方法?
我尝试了什么:
我搜索了这个,并在网站上阅读了几个主题,其中包括几个,搜索过Microsoft Docs和MSDN,但我无法让它工作。
I've made a countdown timer using the timer component to update a label with the remaining time left. The problem is that even though the interval is set to 1 the label doesn't update every millisecond. I've read on different websites that that's because it's not a real time environment.
I've also made a timer that uses DateTime to calculate the difference between the time the timer is started and the actual time, using the same timer component as with the countdown timer. This does update the label every millisecond.
Seeing as the DateTime method does update the label every millisecond I was wondering, is it possible to make a countdown timer with the DateTime method which counts down from a to set amount of time (hours, minutes, seconds, milliseconds)?
What I have tried:
I've searched about this and read several topics on websites, including several on this one, searched Microsoft Docs and MSDN, but I just can't get it to work.
推荐答案
private DateTime endsAt;
...
endsAt = DateTime.Now.AddMinutes(5);
然后在Tick处理程序中计算如何剩下的就是:
Then in the Tick handler calculate how much is left:
void countDown_Tick(object sender, EventArgs e)
{
int secondsRemaining = (int) (endsAt - DateTime.Now).TotalSeconds;
myLabel.Text = secondsRemaining.ToString();
}
这篇关于如何在日期时间倒数计时器中输入时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!