语言

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 毫秒。

可以使用此属性使应用程序的行为取决于应用程序运行的时间长度,也可以使用它来标记事件,这两者都独立于计算机的时钟。

Caution

当属性的值 TickCount 达到最大整数值时(MaxValue),它将跳转到最小整数值(MinValue)、负数,并继续递增。

如果计算机连续运行,则 TickCount 从零增加到大约 24.9 天内的最大整数值。

该 TickCount 属性仅在操作系统运行时递增;当计算机进入某些节能模式(如待机或休眠)时,该属性才会暂停。 该 TickCount 属性与计算机的时钟设置无关。

使用 LocalTime 属性或 GmtTime 属性获取此计算机上的当前本地日期和时间。

该 My.Computer.Clock.TickCount 属性的行为与 Environment.TickCount 属性相同。

按项目类型提供可用性

项目类型 可用的
Windows 应用程序 Yes
类库 Yes
控制台应用程序 Yes
Windows 控件库 Yes
Web 控件库 Yes
Windows 服务 Yes
网站 Yes

适用于

另请参阅