Click here to Skip to main content
Click here to Skip to main content

Timer Functionality without a Timer Control in VB6

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

Introduction

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.

Background

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
        DoEvents
    Loop
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...

WithEvents

... 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.

History

  • 14th September, 2008: Initial post 

License

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

Share

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 | :)
 
Contact: neolithian@msn.com

Comments and Discussions

 
QuestionReset method PinmemberHamo Kupac6-Jan-13 17:36 
Generalmuch CPU Processing PinmemberAiliA.wanted14-Apr-12 7:06 
GeneralRe: much CPU Processing Pinmembergaraber28-Mar-14 6:33 
GeneralMy vote of 5 Pinmembermanoj kumar choubey26-Mar-12 1:59 
GeneralRe: My vote of 5 PinmemberKazi Zakir Hossain1-Apr-12 5:52 
QuestionTimer simultaneity PinmemberDenis Rutovitz10-Dec-11 15:45 
AnswerRe: Timer simultaneity PinmemberKazi Zakir Hossain11-Dec-11 1:35 
GeneralMultiple Timers Pinmembercarlos.hix5-Feb-09 6:53 
AnswerRe: Multiple Timers Pinmemberfoible4-Feb-10 0:31 
GeneralOne question Pinmemberfzhty16-Sep-08 7:31 
GeneralRe: One question PinmemberZakir Hossain17-Sep-08 0:06 
GeneralRe: One question Pinmemberfzhty21-Sep-08 6:48 
GeneralRe: One question Pinmemberfoible4-Feb-10 0:28 
GeneralRe: One question Pinmembersoorya prakash24-Mar-12 14:15 
QuestionRe: One question PinmemberKazi Zakir Hossain25-Mar-12 3:35 

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

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

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