Click to See Complete Forum and Search --> : Timer in Module


eng70640
May 4th, 2001, 01:30 AM
How can i implement a timer in a procedure in one of my modules so that i can impose a delay of 1 second?

cksiow
May 4th, 2001, 01:41 AM
Maybe I don't understand your question, but just get some delay, Sleep API would be able to fulfill the job.

eng70640
May 4th, 2001, 01:58 AM
Hi , u mean that sleep api can implement a delay in any procedure in a module (.bas ) file ?

cksiow
May 4th, 2001, 02:48 AM
yap.

Cimperiali
May 4th, 2001, 03:40 AM
If instead you need a timer event (that means: you need some place where to put code that will be executed form time to time) and you want it without using a timer in a hidden form, then someone else once told me how to do.
Here is an adaption of his code (sorry man, I do not remember who was)
'--------
Module bas code:
Option Explicit

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 g_iTimerCounter As Integer
Public blnTimer As Boolean
Public lngTimerID As Long

Public Sub starttimer()
If blnTimer = False Then
Call mioTimer(False)
End If


End Sub
Public Sub stoptimer()
Call mioTimer(True)
End Sub

Private Sub mioTimer(ByRef blnTimer As Boolean)

On Error GoTo ErrHandler

If blnTimer = False Then
Debug.Print g_iTimerCounter
lngTimerID = SetTimer(0, 0, 500, AddressOf TimerProc)
If lngTimerID = 0 Then
MsgBox "Timer not created. Ending Program"
Exit Sub
End If
blnTimer = True
Ftimer.Caption = "Timer started"

Else
Debug.Print g_iTimerCounter
'lngTimerID = 31307
lngTimerID = KillTimer(0, lngTimerID)
If lngTimerID = 0 Then
MsgBox "couldn't kill the timer"
End If
g_iTimerCounter = 0
blnTimer = False
Ftimer.Caption = "Timer stopped"
End If
Exit Sub
ErrHandler:
MsgBox Err & Error$
Resume Next
End Sub

Sub TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, _
ByVal idEvent As Long, ByVal dwTime As Long)

g_iTimerCounter = g_iTimerCounter + 1
'timer event: to stop timer here enable this
'Call mioTimer(True) 'distruggi il timer

'do your stuff as in timer procedure of a timer

Debug.Print "FaaaAtto!"
exit_form_here:
Screen.MousePointer = vbNormal

Exit Sub
ErrHandler:

Debug.Print Err, Error$

Resume exit_form_here
End Sub
'--------------------------------
'to test, make a form with two command button (first to start, second to stop
'the timer), name it Ftimer and paste this code
'To see event triggered, put a breakpoint in Sub TimerProc
Option Explicit

Private Sub Command1_Click()
MTimer.starttimer
End Sub

Private Sub Command2_Click()
MTimer.stoptimer
End Sub

Private Sub Form_Unload(Cancel As Integer)
If blnTimer = True Then
MTimer.stoptimer
End If

End Sub



Special thanks to Lothar "the Great" Haensler, Tom Archer, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.