倒数计时器工作异常吗? [英] Count down timer works abnormally?

查看:97
本文介绍了倒数计时器工作异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个倒数计时器,将时间显示为"hh:mm:ss",它显示了剩余时间,但不是秒减少1,而是减少2?计时器的间隔设置为1000.这是我的Tick事件处理程序:

I created a countdown timer displaying the time as ''hh:mm:ss'', this shows the remaining time, but instead of the second decreasing by 1, it decreases by 2? The interval of timer is set to 1000. Here is my handler for Tick event:

Private Sub Tick_Handler(ByVal sender As Object, ByVal e As EventArgs) Handles t.Tick
ts -= 1
RemainingTime.Text = ts\3600 & ":" & (ts Mod 3600)\60 & ":" & ts Mod 60
End Sub


ts是以毫秒为单位的时间量.
我不明白ts如何减少2而不是1?
你能帮我解释一下奇怪的事情吗?


ts is the amount of time in miliseconds.
I can''t understand how ts is decreased by 2 instead of 1?
Could you help me explain that strange thing?
Your help would be highly appreciated!

推荐答案

可以在其他地方.只需检查您在哪里还使用成员ts.但是最好不要-整个事情根本没有道理.您正在尝试计算应该认为是一秒的周期中的毫秒数.而且,您为什么认为计时器足够准确.您根本不需要此成员.您始终可以使用System.DateTime.Now获取当前时间.要计算剩余时间,请记住一些时间并通过为类型System.DateTime定义的减法运算符计算剩余时间.
您也不需要将时间转换为分钟,秒等,这已经为您完成了.您可以使用小数形式(双浮点数类型)或整个整数来获取所需的任何单位.如果仅用于在屏幕上显示值,请使用System.DateTime.ToString和适当的格式说明符.

参见:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ].

—SA
It can be somewhere else. Just check where else you use the member ts. But better don''t — the whole thing makes no sense at all. You are trying to count what you think is milliseconds in the cycle of what is supposed to be a second. And also, why do you think the timer is accurate enough. You don''t need this member at all. You can always get current time using System.DateTime.Now. To count remaining time, remember some time and calculate remaining time by subtract operator defined for the type System.DateTime.

You also don''t need to convert time into minutes, seconds, etc. — it is already done for you. You can get any units you need, either in fractional form (double floating-point type) or in whole integer. And if this is just for showing value on screen, use System.DateTime.ToString with appropriate format specifier.

See:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx[^],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

—SA


尝试类似的线程:
倒数计时器
Try similar thread:
Countdown timer


实际上是代码您发布的内容至少在我的系统上运行良好.请检查应用程序其他部分是否存在错误.
Actually the code you posted works fine, at least on my system. Please check if there are bugs in other parts of your application.


这篇关于倒数计时器工作异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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