This documentation is archived and is not being maintained.

My.Computer.Clock.TickCount Property

Gets the millisecond count from the computer's system timer.

' Usage
Dim value As Integer = My.Computer.Clock.TickCount
' Declaration
Public ReadOnly Property TickCount As Integer

An Integer containing the millisecond count from the computer's system timer.

The TickCount property provides access to the computer's system timer, which runs when the computer is active. The timer resolution is not less than 500 milliseconds.

You can use this property to make your application's behavior dependent on the length of time it has been running, or you can use it to label events, both of which are independent of the computer's clock.

Caution noteCaution:

When the value of the TickCount property reaches the maximum integer value (MaxValue), it then jumps to the minimum integer value (MinValue), a negative number, and continues to increment.

If the computer runs continuously, TickCount increments from zero to the maximum integer value in approximately 24.9 days.

The TickCount property increments only when the operating system is running; it pauses when the computer goes into certain power-saving modes, such as standby or hibernation. The TickCount property is unrelated to the computer's clock setting.

Use the My.Computer.Clock.LocalTime Property or My.Computer.Clock.GmtTime Property to obtain the current local date and time on this computer.

The My.Computer.Clock.TickCount property has the same behavior as the Environment.TickCount property.

The following example uses the My.Computer.Clock.TickCount property to run a task in a loop for a given number of seconds, even if the computer's system time changes while it runs.

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

Namespace: Microsoft.VisualBasic.Devices

Class: Clock

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type

Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

Yes

Windows Service

Yes

Web Site

Yes

No permissions are required.

Show: