PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: jermy on July 08, 2019, 05:08:46 PM

Title: Delay Function? Better then Sleep
Post by: jermy on July 08, 2019, 05:08:46 PM
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.

Code: [Select]

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"

Title: Re: Delay Function? Better then Sleep
Post by: raymw on July 08, 2019, 08:43:41 PM
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...
Title: Re: Delay Function? Better then Sleep
Post by: SeaVipe on July 09, 2019, 12:17:00 AM
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?
Title: Re: Delay Function? Better then Sleep
Post by: jermy on July 09, 2019, 11:55:18 AM
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.

Code: [Select]
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