VB.NET 定时器间隔 1 = 1 毫秒?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11031600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET Timer Interval 1 = 1 millisecond?
提问by nnm
I Got a Timer With Interval = 1
我有一个时间间隔 = 1 的计时器
Interval 1 = 1 millisecond ?
if Interval 1 is not 1 millisecond ,
So tell me which control interval = 1ms
间隔 1 = 1 毫秒?
如果间隔 1 不是 1 毫秒,
那么告诉我哪个控制间隔 = 1ms
code:
代码:
Imports System.Globalization
Public Class Form1
'Default Time To Start From
Dim time As String = "00:00:00,000" 'Start From Here
'Label
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
Timer1.Start() 'Run The Timer On Click
End Sub
'TIMER
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Timer Interval = 1
Dim ci = CultureInfo.InvariantCulture
Dim original As TimeSpan = TimeSpan.ParseExact(time, "hh\:mm\:ss\,fff", ci) 'ParseExact
Dim difference As TimeSpan = TimeSpan.FromMilliseconds(1) ' = 1 Millisecond
Dim final = original
final = original + difference ' connect between original to difference !(00:00:00,000 + 1 MS = 00:00:00,001)!
Dim output As String = final.ToString("hh\:mm\:ss\,fff", ci) 'convert to the format ( = 00:00:00,001 ) (Back It To The Right Format)
time = output '!!Update!! the Time String from 00:00:00,000 To 00:00:00,001 |||| And in the Next Time 00:00:00,001 + 1 = 00:00:00,002
Label1.Text = time 'Show the Time String in the label
End Sub
End Class
As you see - Im Useing Regular TimerWith Interval 1 ,
But i think that it worng because timer not counting milliseconds
if you got advice ,tell me.
如您所见 -我使用间隔为 1 的常规计时器,
但我认为它很糟糕,因为
如果您有建议,请告诉我计时器不计算毫秒。
回答by Jon Egerton
The description of the interval property from MSDN is:
MSDN 中对 interval 属性的描述是:
Gets or sets the time, in milliseconds, before the Tick event is raised relative to the last occurrence of the Tick event.
获取或设置相对于上次发生的 Tick 事件在引发 Tick 事件之前的时间(以毫秒为单位)。
However (as Steve pointed out in his comment):
但是(正如史蒂夫在评论中指出的那样):
The Windows Forms Timer component is single-threaded, and is limited to an accuracy of 55 milliseconds. If you require a multithreaded timer with greater accuracy, use the Timer class in the System.Timers namespace
Windows Forms Timer 组件是单线程的,精度限制为 55 毫秒。如果需要更准确的多线程计时器,请使用 System.Timers 命名空间中的 Timer 类
Taken from http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx
取自http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx
The System.Timers.Timer
class referred to is described here: http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
System.Timers.Timer
此处描述了所指的类:http: //msdn.microsoft.com/en-us/library/system.timers.timer.aspx
回答by user8641161
Imports System.Globalization
Public Class Form1
Dim time As String = "00:00:00:00"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim ci = CultureInfo.InvariantCulture
Dim original As TimeSpan = TimeSpan.ParseExact(time, "dd\:hh\:mm\:ss", ci) 'ParseExact
Dim difference As TimeSpan = TimeSpan.FromSeconds(1) ' = 1 Millisecond
Dim final = original
final = original + difference ' connect between original to difference !(00:00:00,000 + 1 MS = 00:00:00,001)!
Dim output As String = final.ToString("dd\:hh\:mm\:ss", ci) 'convert to the format ( = 00:00:00,001 ) (Back It To The Right Format)
time = output '!!Update!! the Time String from 00:00:00,000 To 00:00:00,001 |||| And in the Next Time 00:00:00,001 + 1 = 00:00:00,002
Label1.Text = time 'Show the Time String in the label
End Sub
End Class
Here's working script. IDK why it works but it works! :)
这是工作脚本。IDK 为什么它有效但它有效!:)