語言

Clock.TickCount 屬性

定義

從電腦系統計時器讀取毫秒計數。

public:
 property int TickCount { int get(); };
public int TickCount { get; }
member this.TickCount : int
Public ReadOnly Property TickCount As Integer

屬性值

包含 Integer 電腦系統計時器的毫秒計數。

範例

以下範例利用了 My.Computer.Clock.TickCount 這個特性,即使電腦在執行過程中系統時間改變,也將任務以循環方式執行給定的秒數。

Public Sub LoopTask(ByVal secondsToRun As Integer)
    Dim startTicks As Integer = My.Computer.Clock.TickCount
    Do While IsTimeUp(startTicks, secondsToRun)
        ' Code to run for at least secondsToRun seconds goes here.
    Loop
End Sub

Private Function IsTimeUp(
    ByVal startTicks As Integer,
    ByVal seconds As Integer
) As Boolean
    ' This function throws an overflow exception if the
    ' tick count difference is greater than 2,147,483,647,  
    ' about 24 days for My.Computer.Clock.TickCount.

    ' Use UInteger to simplify the code for roll over.
    Dim uStart As UInteger =
        CUInt(CLng(startTicks) - Integer.MinValue)
    Dim uCurrent As UInteger =
        CUInt(CLng(My.Computer.Clock.TickCount) - Integer.MinValue)

    ' Calculate the tick count difference.
    Dim tickCountDifference As UInteger
    If uStart <= uCurrent Then
        tickCountDifference = uCurrent - uStart
    Else
        ' Tick count rolled over.
        tickCountDifference = UInteger.MaxValue - (uStart - uCurrent)
    End If

    ' Convert seconds to milliseconds and compare.
    Return CInt(tickCountDifference) < (seconds * 1000)
End Function

備註

該 TickCount 特性提供存取電腦系統計時器的存取權,當電腦啟動時計時器會運行。 計時器解析度不少於500毫秒。

你可以利用這個特性讓應用程式的行為依賴於它運行的時間長短,或者用它來標記事件,這兩者都與電腦的時鐘無關。

注意事項

當性質 TickCount 的值達到最大整數值(MaxValue),就會跳到最小整數值(MinValue),一個負數,並繼續遞增。

若電腦持續運行, TickCount 則從零增加到最大整數值約需24.9天。

TickCount這個特性只有在作業系統運行時才會遞增;當電腦進入某些省電模式(如待機或休眠)時,屬性會暫停。 這個 TickCount 屬性與電腦的時鐘設定無關。

使用該 LocalTime 物業 GmtTime 或物業在這台電腦上取得當地的當前日期和時間。

該 My.Computer.Clock.TickCount 物件的行為與物件 Environment.TickCount 相同。

依專案類型提供可用性

專案類型 Available
Windows 應用程式 Yes
類別庫 Yes
主控台應用程式 Yes
Windows 控件庫 Yes
Web 控制庫 Yes
Windows 服務 Yes
網站 Yes

適用於

另請參閱