Share via


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

可用性(按项目类型)

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

适用于

另请参阅