• Welcome to PlanetSquires Forums.
 

Delay Function? Better then Sleep

Started by jermy, July 08, 2019, 05:08:46 PM

Previous topic - Next topic

jermy

Hello people

I need a good delay function to keep everything running smoothly ... any suggestions?
the code below works, but the time entered is very different.



Dim Count as Long

For COUNT = 1 To 100
Sleep 100         
FF_DoEvents              'necessary so that the form does not freeze
Next COUNT

Print "end sleep"


raymw

Could you use the timer or time function. If the time you are getting is consistently different from that which you expect, then you need to allow for the loop counting. If the difference is random, then that is probably windows deciding to do something like checking the mouse, or what the weather is doing...

SeaVipe

Hi Jermy,


What is your "delay" used for? Ray's idea of a Timer may just do what you need. Can your delay routine run in a separate thread?
Clive Richey

jermy

#3
I think the code I came up with works well and does not have a disruptive effect.
I have also not seen a high CPU load.
I need the code for a game, the timing is pretty tight so that a good interaction takes place.


Sub MsgWait(ByVal MilliSeconds as DWORD)
 
  Dim start as DWORD, timeRemaining as DWORD, timeNow as DWORD
  Dim uMsg as Msg
 
  start = GetTickCount()
  timeRemaining = MilliSeconds
  Do
     If PeekMessage(VarPtr(uMsg), 0, 0, 0, PM_REMOVE) <> 0 Then
       If IsDialogMessage(GetActiveWindow, VarPtr(uMsg)) = 0 Then
          TranslateMessage VarPtr(uMsg)
          DispatchMessage VarPtr(uMsg)
       End If   
    End If

    timeNow = GetTickCount()
    If timeNow - start >= timeRemaining Then
      Exit Sub
    ElseIf timeNow < start Then
      ' Handle GetTickCount 49.7 day wrap around
      start = timeNow
    End If
    timeRemaining = timeRemaining - (timeNow - start)
    start = timeNow
  Loop

End Sub