Click here to Skip to main content
13,345,372 members (52,825 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 14 Sep 2008

Timer Functionality without a Timer Control in VB6

, 14 Sep 2008
Rate this:
Please Sign up or sign in to vote.
A VB6 project describing how to implelement timer functionality without a timer control


Think that you have a Microsoft Excel application (VBA) and you have to use timer functionality there. There is no such native control in Excel called "Timer". What will you do? Also think that you are in a situation where you are unable to use a "Form" where you can "put" a timer control. But still you need a timer functionality that will work exactly like the timer control. It'll fire an event in a timely fashioned and can start and stop, etc. If you're in such a situation, the article below is for you.


I have seen many articles on the Internet that resolve timer functionality without a timer control using a set of complicated Win32 APIs. The codes are very risky to use and your application can crash anytime. Also in most of the cases, there is no event firing option like the timer control has. In this article, I have shown a very simple way to implement timer functionality without a timer control just using a single Win32 API. It is a single class AxtiveX DLL project.

Using the Code

The main tricky way of this application is using a Win32API named "GetTickCount". The GetTickCount function retrieves the number of milliseconds that have elapsed since Windows was started. Using this API, you can code an ActiveX DLL that will serve you the need of a timer. I have named the project TimerLib and it has only a single class named TimerEx.cls. See the code below:

Option Explicit

'* The GetTickCount function retrieves the number of milliseconds
'* that have elapsed since Windows was started.
Private Declare Function GetTickCount Lib "kernel32" () As Long

Private mblnEnabled             As Boolean
Private mlngInterval            As Long
Private mstrTag                 As String
Private mlngTickCount           As Long
Private mtypIntervalType        As IntervalData

'* This is the timer event that will fire in a given interval
Public Event OnTimer()

'* A type that will hold the extended information about the interval you want to set
'* If you set different types of intervals, the total interval will
'* be calculated combining all types
Public Type IntervalData
    MilliSecond As Long
    Second As Long
    Minute As Long
    Hour As Long
End Type

'* You can see whether the timer is Enabled by this property
Public Property Get Enabled() As Boolean
    Enabled = mblnEnabled
End Property

'* You can start / stop the timer by this property
Public Property Let Enabled(blnEnabled As Boolean)
    mblnEnabled = blnEnabled
    If blnEnabled Then
        mlngTickCount = GetTickCount
        Call TimerLoop
    End If
End Property

'* Conventional Interval property of the timer, you can check how many milliseconds
'* have been set for the timer
Public Property Get Interval() As Long
    Interval = mlngInterval
End Property

'* Conventional Interval property of the timer, you can set interval of the timer
'* in milliseconds
Public Property Let Interval(lngInterval As Long)
    mlngInterval = lngInterval
End Property

'* Extended Interval property of the timer, you can check how many
'* milliseconds / seconds / minutes / hours have been set for the timer
Public Property Get IntervalInfo() As IntervalData
    IntervalInfo = mtypIntervalType
End Property

'* Extended Interval property of the timer, you can set the interval in
'* milliseconds / seconds / minutes / hours
Public Property Let IntervalInfo(typIntervalType As IntervalData)
    mtypIntervalType = typIntervalType
    mlngInterval = mtypIntervalType.MilliSecond + typIntervalType.Second * 1000 + _
	typIntervalType.Minute * 60 * 1000 + typIntervalType.Hour * 60 * 60 * 1000
End Property

'* Check what info is in the Tag property in the timer, you can store any string data
'* into this property
Public Property Get Tag() As String
    Tag = mstrTag
End Property

'* You can store any string data into this property as extra info of your timer
Public Property Let Tag(strTag As String)
    mstrTag = strTag
End Property

'* Core of the timer. It fires the OnTimer event in a timely fashion according to
'* the Interval / IntervalInfo you have set
Private Sub TimerLoop()
    Do While Not mblnEnabled = False
        If GetTickCount - mlngTickCount >= mlngInterval Then
            RaiseEvent OnTimer
            mlngTickCount = GetTickCount
        '* Like GetTickCount has exceeded its capacity,
        '* run over from the beginning
        ElseIf GetTickCount = 0 Then
            mlngTickCount = 0
        ElseIf GetTickCount < mlngTickCount Then
            mlngTickCount = 0
        End If
End Sub

'* ENJOY!!	

How to Use the Library

You can use this library from any COM compatible high level language. In the sample code, I have taken a standard VB6 EXE application that has two buttons in the form named "cmdEnableTimer" and "cmdDisableTimer". I have taken the reference of the timer library from the Project>References. and used the TimerEx class using...


... Keyword so that the timer event can be tracked. In my library, the timer event is OnTimer. See the code below:

Option Explicit

Private WithEvents myTimer  As TimerEx
Private mlngTick            As Long
Private myIntervalInfo      As IntervalData

Private Sub cmdDisableTimer_Click()
    myTimer.Enabled = False
End Sub

Private Sub cmdEnableTimer_Click()
    myTimer.Enabled = True
End Sub

Private Sub Form_Load()
    myIntervalInfo.Second = 5
    Set myTimer = New TimerLib.TimerEx
    myTimer.IntervalInfo = myIntervalInfo
End Sub

Private Sub Form_Unload(Cancel As Integer)
    myTimer.Enabled = False
    Set myTimer = Nothing
End Sub

Private Sub myTimer_OnTimer()
    mlngTick = mlngTick + 1
    Me.Caption = mlngTick
End Sub

Points of Interest

You can use the TimerLib from any COM compatible language. And you'll see from the code that here I extended the Interval property of the VB6 timer control from conventional milliseconds to Second / Minute and even in Hour. Hope you'll enjoy using this code.


  • 14th September, 2008: Initial post 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kazi Zakir Hossain
Software Developer (Senior) Orion Informatics Ltd.
Bangladesh Bangladesh
I have been doing computer programming for about last 12 years. Started practicing with QBASIC 4.5. Though started loving VB 4.0 quickly. I worked for national organization like BRAC and still working for international organization like British Council. Once I used to develop web applications with classic ASP 3.0. In my long programming journey I developed many customised user controls in VB6 and many custom functionaly enriched library to meet client requirements. Right now doing ASP.NET and C#. (Also did not stop coding in VB6). Also learning WPF, WCF and SilverLight in .NET 4. Anyway, life is not that bad Smile | :)


You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey26-Mar-12 1:59
membermanoj kumar choubey26-Mar-12 1:59 
GeneralRe: My vote of 5 Pin
Kazi Zakir Hossain1-Apr-12 5:52
memberKazi Zakir Hossain1-Apr-12 5:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 14 Sep 2008
Article Copyright 2008 by Kazi Zakir Hossain
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid