Recent Posts

Sunday, June 29, 2008

Win API Timer in VB 6.0

If you need to execute the particular piece of code in certain duration in your vb 6.0 application without using the vb Timer control then this code sample will help you a lot.

This API call doesn't uses the unwanted cpu processor but it only executes the Function or a sub at a given time and closes the timer when ordered.

Fist in the module put the following code:

Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, _
ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, _
ByVal nIDEvent As Long) As Long

Public lngIden As Long

Public Function StartTimer()

lngIden = SetTimer(0, 0, 5000, AddressOf TimerCallback)

End Function

Public Function CloseTimer()

KillTimer 0, lngIden

End Function

Public Function TimerCallback(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent _
As Long, ByVal dwTime As Long) As Long

Dim lngRetVal As Long
lngRetVal = MsgBox("Hi its' me making a callback! Want me to prompt again?", _
vbExclamation + vbYesNo, "CallBack Functions")

If lngRetVal = vbNo Then
Call CloseTimer
End If

End Function

Now call the functions from the form

Private Sub Form_Load()
Call StartTimer
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call CloseTimer
End Sub

The Code sample can be downloaded and can be used from the following link below:

If any comments or add ons on the code then please don't hesisate to comment on this blog.

Related Posts by Categories


Anonymous said...

Hi, so the following sentence: lngIden = SetTimer(0, 0, 5000, AddressOf TimerCallback); determines the countercount for the msgbox to appear, which is 5 sec right? I need to set this time for 30 days and delete a password from a user in a access 2007 db. Can you guide me on how to do this? thanks

rprateek said...

ya just calculate how much time it requires for 30 days and use it. Then in the function that you are pointing to write the delete password sql command and execute it.

It should work

Prabu Nivas said...

I just run through your blog its really cool, I am new to VB. I developing small application in VB. Retrieving Excel data and i need to convert those data into XML file. Its possible from VB, I am awaiting for your response.

Thanks and Regards

Post a Comment