I have a loop going. But between each loop I want it to wait 5 seconds. How would I do that? (before it goes on to loop again it has to wait 5 seconds)
Printable View
I have a loop going. But between each loop I want it to wait 5 seconds. How would I do that? (before it goes on to loop again it has to wait 5 seconds)
Code:Public Function DelayLoop()
Dim intDelay As Integer ' Delay in seconds
Dim dtmTime As Date
Dim i As Integer
intDelay = 5
For i = 1 To 10
' Do your stuff here
Beep
dtmTime = Now()
Do Until DateDiff("s", dtmTime, Now()) >= intDelay
DoEvents
Loop
Next
End Function
You can use the API call Sleep
General Declarations
ImplementationCode:Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Code:Sleep 5000 ' Wait 5 seconds
If you're doing some loop function, you should not applied the Sleep API function call. Because it will cause your entire application fall in idle mode.
So, you should use the GetTickCount API function call.
Code:Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
Dim i As Integer
Do While i <> 100
i = i + 1
DELAY 5000
DoEvents
Loop
End Sub
Private Sub DELAY(ByVal interval As Long)
Dim t1 As Long
Dim t2 As Long
t1 = GetTickCount
t2 = 0
While t2 - t1 < inteval
t2 = GetTickCount
DoEvents
Wend
End Sub
Chris,
What is idle mode and what are the disadvantages of your program 'falling' into it??
Hi! YoungBuck, Idle mode mean the application will stop process any window message. That mean, the application will only resume all the on queue window message after the sleep interval.
Whereas, if you use another Do...Loop with GetTickCount API function, the application will be able to continue process the window message. As a result the user can close the application by click on the "X" button at anytime he/she wish.
Cool :cool: I didn't know that had a name for that :D
don't worry you guys, I did it a totally different way. I didn't use any of your ideas. I actually found out how to use the timer and loop together. When the button is clicked, it calls another Private Function which enables the timer. After the timer is enabled it waits 5 seconds then it does everything and disables itself and then jumps to the Private Function. which I used an if statement, so if the conditions are right it will enabled the timer. Kind of complicated but it works. If you have any more questions you could email me [email protected]
--------------------------------------------------------
=-> Hot2fire <-=
http://ksearch.cjb.net - Search with results
Timers aren't good if your program is large. The timers start to get inacurate. By the way, Fox will yell at you if he finds you using them ;) he hates timers and picture boxes.
Try to stay away from timers as much as possible. GetTickCount is more acurate anyway. :)