Results 1 to 6 of 6

Thread: Best way to slow down a for loop

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    3

    Question

    Hi All.
    I'm looking for the best way to slow down a loop.
    Is using a timer the best way to go.
    Seems like a lot of overhead. I have done non productive
    equations to slow things down also not good as it will
    very a lot depending on the speed of the processor the
    app is run on.

    Any help greatfully accepted.

  2. #2
    Lively Member
    Join Date
    Aug 2000
    Posts
    125
    this works best for me - Spd is like 7000

    Code:
    Private Sub pause()
        Dim i As Long
        
        For i = 1 To Spd
            DoEvents
        Next
    End Sub
    there is an example at:
    http://pages.about.com/vbmakai/sv1.htm

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    3

    Thanks Makai

    Thanks Makai for the fast reply.
    Used it seems to work fine.
    Damien :-)

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Code:
    Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long
    
    Private Sub pause(milliseconds as long)
        Dim i As Long
        i=Gettickcount+milliseconds
        Do while Gettickcount<i
            DoEvents
        loop
    End Sub
    This ones a bit more adcanced, you can put the exact amount of millisecond for it to pause
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    3

    Wink

    Thanks Kedaman
    I looked up GetTickCount and found that it rolls over to
    zero if the system has been running for 49.7 days(obviously
    not running a MicroSoft OS). Still stranger things have
    happened. The pause routine could go into an infinite loop
    if it was running when a system rolled over.
    May be worth testing if count has just rolled over.

    Once again thankyou for the advice
    Damien

    Code:
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    
    Private Sub pause(milliseconds As Long)
        Dim i As Long
        i = GetTickCount + milliseconds
        Do While GetTickCount < i
            If GetTickCount < 3000 Then Exit Do 'TickCount loops over to zero every 49.7 days
            DoEvents
        Loop
    End Sub

    [Edited by Damien on 10-16-2000 at 11:58 PM]

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    how about 29247 years!

    Yeah, not only if you have running your system for that long, but also you can pause like some years if you want, hehehe
    Code:
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    
    Sub pause(milliseconds As Currency)
        Dim i As Currency, j As Currency, k As Currency
        j = GetTickCount
        i = j + milliseconds
        Do While (j + k) < i
            If GetTickCount < j Then k = k + j
            j = GetTickCount
            DoEvents
        Loop
    End Sub
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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