Edit

Share via


Clock.TickCount Property

Definition

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

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

Property Value

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

Examples

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

Remarks

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

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 LocalTime property or 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.

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

Applies to

See also