PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Delay Function? Better then Sleep  (Read 285 times)


  • Little Newbie
  • *
  • Posts: 22
Delay Function? Better then Sleep
« 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

Print "end sleep"



  • Senior Member
  • ***
  • Posts: 323
Re: Delay Function? Better then Sleep
« Reply #1 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...


  • Junior Member
  • **
  • Posts: 194
  • Windows 10
Re: Delay Function? Better then Sleep
« Reply #2 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?
Clive Richey
There is nothing government can give you that it hasn't already taken from you in the first place. Winston Churchill


  • Little Newbie
  • *
  • Posts: 22
Re: Delay Function? Better then Sleep
« Reply #3 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
     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

End Sub
« Last Edit: July 09, 2019, 11:57:43 AM by jermy »