Results 1 to 9 of 9

Thread: Timed Loop

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2000
    Location
    Faraway from Osama bin Laden <--- Loser
    Posts
    42

    Arrow

    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)

  2. #2
    Hyperactive Member
    Join Date
    Oct 2000
    Posts
    400
    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

  3. #3
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987

    API

    You can use the API call Sleep

    General Declarations
    Code:
    Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
    Implementation
    Code:
    Sleep 5000 ' Wait 5 seconds
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    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

  5. #5
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987

    ???

    Chris,
    What is idle mode and what are the disadvantages of your program 'falling' into it??
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  6. #6
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    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.


  7. #7
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    Cool I didn't know that had a name for that
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  8. #8

    Thread Starter
    Member
    Join Date
    Dec 2000
    Location
    Faraway from Osama bin Laden <--- Loser
    Posts
    42

    Lightbulb

    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

  9. #9
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width