Click here to Skip to main content
15,867,141 members
Articles / Desktop Programming / Win32

Timer Functionality without a Timer Control in VB6

Rate me:
Please Sign up or sign in to vote.
4.64/5 (11 votes)
14 Sep 2008CPOL2 min read 112.3K   2.6K   16   16
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:

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

VB.NET
WithEvents

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

VB.NET
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)


Written By
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 Pin
Hamo Kupac6-Jan-13 16:36
Hamo Kupac6-Jan-13 16:36 
Generalmuch CPU Processing Pin
Max pk14-Apr-12 6:06
Max pk14-Apr-12 6:06 
GeneralRe: much CPU Processing Pin
garaber28-Mar-14 5:33
garaber28-Mar-14 5:33 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Mar-12 0:59
professionalManoj Kumar Choubey26-Mar-12 0:59 
GeneralRe: My vote of 5 Pin
Kazi Zakir Hossain1-Apr-12 4:52
Kazi Zakir Hossain1-Apr-12 4:52 
QuestionTimer simultaneity Pin
Denis Rutovitz10-Dec-11 14:45
Denis Rutovitz10-Dec-11 14:45 
AnswerRe: Timer simultaneity Pin
Kazi Zakir Hossain11-Dec-11 0:35
Kazi Zakir Hossain11-Dec-11 0:35 
GeneralRe: Timer simultaneity Pin
Felix Dombek7-Feb-17 7:31
Felix Dombek7-Feb-17 7:31 
GeneralMultiple Timers Pin
carlos.hix5-Feb-09 5:53
carlos.hix5-Feb-09 5:53 
AnswerRe: Multiple Timers Pin
foible3-Feb-10 23:31
foible3-Feb-10 23:31 
GeneralOne question Pin
fzhty16-Sep-08 6:31
fzhty16-Sep-08 6:31 
GeneralRe: One question Pin
Kazi Zakir Hossain16-Sep-08 23:06
Kazi Zakir Hossain16-Sep-08 23:06 
GeneralRe: One question Pin
fzhty21-Sep-08 5:48
fzhty21-Sep-08 5:48 
GeneralRe: One question Pin
foible3-Feb-10 23:28
foible3-Feb-10 23:28 
GeneralRe: One question Pin
soorya prakash24-Mar-12 13:15
soorya prakash24-Mar-12 13:15 
QuestionRe: One question Pin
Kazi Zakir Hossain25-Mar-12 2:35
Kazi Zakir Hossain25-Mar-12 2:35 

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.