Create a system timer using AddressOf and a callback function

Create a system timer using AddressOf and a callback function

The Timer control is great when you want to periodically execute a piece of code while the program is doing something else. However, it also has a couple of shortcomings: (1) it requires a parent form, so you can’t use it directly inside a BAS module, and (2) it’s Interval property can’t be higher than 65,535, therefore you can’t specify a timeout longer than about 65 seconds.

You can work around both the above limitations if you directly create a system timer using the SetTimer function. When you create a timer in this way you provide the address of a callback function, a function in a BAS module that Windows will call periodically. You need the following API declares to create and then distroy a system timer:

Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, _    ByVal nIDEvent    As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) _    As LongDeclare Function KillTimer Lib "user32" (ByVal hWnd As Long, _    ByVal nIDEvent As Long) As Long

This code creates a timer that is invoked every 500 milliseconds:

Dim timerID As Long' the first two arguments must be zerotimerID = SetTimer(0, 0, 500, AddressOf Timer_CBK)

Note that you need a module-level or a global timerID variable to store the ID of the timer you create, because you must absolutely destroy the timer before the application terminates (or when you don’t need it):

' Destroy the timer created previouslyKillTimer 0, timerID

Here is an example of a timer callback routine:

' the fourth argument is the number of milliseconds elapsed ' from Windows start upSub Timer_CBK(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, _    ByVal SysTime As Long)    ' In this example just display the system time in a label control    Form1.lblTimer = SysTimeEnd Sub

For simplicity’s sake, the above example uses this timer to display the number of milliseconds elapsed since Windows started in a Label control in a form (which is a job for which a regular Timer control is probably a better choice). In a real application you can use this technique for more interesting tasks, such as monitoring data coming from a serial port, checking your email account, watching the contents of a directory, updating program’s statistics, and more.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes