Set your Timer Interval to something less than one second;
(将您的计时器间隔设置为小于一秒;)
I used 250. (我用了250)
Then store the time in the future that is XXX seconds away, representing your countdown duration.
(然后将未来的时间存储在XXX秒之外,代表您的倒计时持续时间。)
At each tick, simply subtract the current time from the stored future time to get a TimeSpan.
(在每个刻度上,只需从存储的未来时间中减去当前时间即可获得TimeSpan。)
Update your label with the TimeSpan value using ToString(). (使用ToString()使用TimeSpan值更新标签。)
When the HowLong property is changed, update the target time and restart your timer...easy peesy.
(更改HowLong属性后,请更新目标时间并重新启动计时器...容易犯错误。)
All together, it'd look something like this:
(总之,看起来像这样:)
Public Class UserControl1
Private target As DateTime
Private setSecs As Integer = 60
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
target = DateTime.Now.AddSeconds(HowLong)
Timer.Start()
End Sub
Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
Dim ts As TimeSpan = target.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
Label1.Text = "-" & ts.ToString("mm:ss")
Else
Label1.Text = "00:00"
Timer.Stop()
End If
End Sub
Property HowLong As Integer
Get
Return setSecs
End Get
Set(value As Integer)
setSecs = value
Timer.Stop()
target = DateTime.Now.AddSeconds(HowLong)
Timer.Start()
End Set
End Property
End Class
The authors response:
(作者回应:)
Technically your way will work to I will post my solution below I did it slightly differently.
(从技术上讲,您的方法将起作用,我将在稍稍不同的地方发布我的解决方案。)
From my comments on the authors own submission:
(根据我对作者本人提交的意见的评论:)
The problem with this type of approach is that the Timer control is not accurate.
(这种方法的问题是Timer控件不准确。)
It is only guaranteed to not fire before the interval has transpired. (仅保证在间隔发生之前不会触发。)
In fact it will almost always fire after the interval with some extra "slop". (实际上,在间隔之后,它几乎总是会发出一些额外的“倾斜”信号。)
For short periods (seconds/minutes), you won't notice. (在短时间内(秒/分钟),您不会注意到。)
For longer periods (hours), you will, as the accumulated slop becomes bigger as time passes. (对于更长的时间(小时),随着时间的流逝,累积的斜率会变大,您会这样做。)
Whether this matters is completely dependent upon your application. (这是否重要完全取决于您的应用程序。)
– Idle_Mind (– Idle_Mind)
Technically speaking, here's a quick example of how inaccurate simply incrementing/decrementing a counter using a 1 second Timer can be:
(从技术上讲,这是一个简单的示例,说明使用1秒计时器简单地增加/减少计数器的不精确度可能是:)
' Timer1.Interval was set to 1000 (timer fires every "second")
Private seconds As Integer = 0
Private start As DateTime = DateTime.Now
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
seconds = seconds + 1
Label1.Text = seconds
Label2.Text = DateTime.Now.Subtract(start).TotalSeconds
End Sub
After only 1 hour and 15 minutes, the counter method on the left is already off by 4 seconds from the actual time that has passed:
(仅1小时15分钟之后,左侧的计数器方法已经比实际经过的时间缩短了4秒:)
A key advantage of the DateTime/TimeSpan method is that the time calculation is independent from the Timer.
(DateTime / TimeSpan方法的主要优点是时间计算独立于Timer。)
That is to say that the frequency at which the Timer fires has no bearing on how accurate the time calculation is. (也就是说,计时器触发的频率与时间计算的准确性无关。)