I'd like to bring up the SetTimer/KillTimer API here. Although I don't know about its accuracy (which seems to be pretty good, however), it does not have the flaws of the timer control and it does not hang the application. Good programming will use the time to at least perform some DoEvents while waiting for the timer elapse.
Put this code in a module to experiment with. The timer API will not work within a Form because of the CallBack routine. I have added a little sample to show how you use the timer API to replace the Sleep() API.
Code:Option Explicit Private Elapsed As Boolean 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 Private TimerID As Long Private Sub StartTimer(ByVal IntervalTime as long) TimerID = SetTimer(0, 0, IntervalTime, AddressOf TimerEvent) End Sub Public Sub TimerEvent(hwnd As Long, msg As Long, idTimer As Long, dwTime As Long) 'if it is a one shot then kill the timer here KillTimer 0, TimerID Elapsed = True 'signal that time is elapsed 'other wise the following is called in the interval you have specified 'timed code goes here End Sub 'this is a sample 'sleep' replacement Public Sub TSleep(SleepTime as Long) StartTimer SleepTime While Not Elapsed DoEvents Wend End Sub




Reply With Quote