2012-06-14 201 views
0

I got a Timer With Interval = 1
VB.NET定時器間隔1 = 1毫秒?

區間1 = 1毫秒?

如果間隔1不爲1毫秒,
所以,告訴我它控制間隔爲1ms

代碼:

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 

正如你看到的 - 林期運用定期定時帶間隔1 ,
但我認爲它worng,因爲計時器不包括毫秒

如果你有一個dvice,告訴我。

+5

[從MSDN](http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx)Windows窗體計時器組件是單線程的,並且是有限的精確到55毫秒。如果您需要更高精度的多線程定時器,請使用System.Timers命名空間中的Timer類。「 – Steve

+0

」您有System.Timers的示例嗎?我需要定時器(1ms) – nnm

+0

向我們展示創建定時器並開始計時的代碼。它可能在'Main'中。它會包含類似於'Timer1.Interval = 1;'的內容。 –

回答

1

從MSDN的間隔屬性的描述是:

獲取或設置時間,以毫秒爲單位,前Tick事件是 凸起相對於Tick事件的最後一個匹配。

但是(如史蒂夫在他的評論中指出):

Windows窗體Timer組件是單線程的,而且是有限的 到55毫秒的精度。如果你需要一個多線程 計時更精確,使用Timer類的System.Timers 命名空間

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

System.Timers.Timer類兩者所指的描述如下:http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

+2

@Steve:對不起 - 直到我發佈了這個消息之後纔看到您的評論(屏幕頂部!)。已經給予了信貸。 –

-1
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 

這是工作腳本。 IDK爲什麼有效,但它的工作原理! :)