Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Create a system timer using AddressOf and a callback function

Total Hit ( 4105)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, _
  ByVal nIDEvent  As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) _
  As Long
Declare 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:

Click here to copy the following block
Dim timerID As Long
' the first two arguments must be zero
timerID = 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 previously

Click here to copy the following block
KillTimer 0, timerID

Here is an example of a timer callback routine:

Click here to copy the following block
' the fourth argument is the number of milliseconds elapsed
' from Windows start up
Sub 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 = SysTime
End 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.



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.