更新:2007 年 11 月
从计算机的系统计时器中获取以毫秒为单位的计数。
' Usage
Dim value As Integer = My.Computer.Clock.TickCount
' Declaration
Public ReadOnly Property TickCount As Integer
返回值
一个 Integer,包含计算机系统计时器中以毫秒为单位的计数。
备注
TickCount 属性提供了对计算机系统计时器的访问,该计时器将在计算机处于活动状态时运行。计时器分辨率不小于 500 毫秒。
可以使用此属性来使应用程序的行为依赖于其已运行的时间长度,也可以使用它来标注事件,这两种用法都与计算机的时钟无关。
![]() |
---|
当 TickCount 属性的值达到最大整数值 (MaxValue) 时,它随后会跳到最小整数值(MinValue,一个负数),并继续递增。 |
如果计算机持续运行,TickCount 将在大约 24.9 天内从零递增到最大整数值。
只有在操作系统运行时,TickCount 属性才会递增;当计算机进入某种节能模式(如待机或休眠)时,它将会暂停。TickCount 属性与计算机的时钟设置无关。
使用 My.Computer.Clock.LocalTime 属性或 My.Computer.Clock.GmtTime 属性来获取此计算机上的当前本地日期和时间。
My.Computer.Clock.TickCount 属性的行为与 Environment.TickCount 属性的相同。
示例
下面的示例将使用 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
要求
命名空间:Microsoft.VisualBasic.Devices
类:Clock
**程序集:**Visual Basic 运行库(位于 Microsoft.VisualBasic.dll 中)
按项目类型列出的可用性
项目类型 |
可用 |
---|---|
Windows 应用程序 |
是 |
类库 |
是 |
控制台应用程序 |
是 |
Windows 控件库 |
是 |
Web 控件库 |
是 |
Windows 服务 |
是 |
网站 |
是 |
权限
不需要任何权限。