?
Solved

Slowing down a loop

Posted on 2003-03-27
7
Medium Priority
?
162 Views
Last Modified: 2010-05-01
Is there a way to slow down a loop so you can actually see it process?  For example I want a program to count down from 10 - 1 and I can do this in a loop, but it will go to 10 straight away.  Is there anyway in slowing this down?

Thanks!

0
Comment
Question by:n-generation
7 Comments
 
LVL 28

Expert Comment

by:vinnyd79
ID: 8217967
use the sleep api in your loop

Private Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)


' pause for 3 seconds
Sleep 3000
0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 200 total points
ID: 8217990
Private Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)

Private Sub Command1_Click()
Dim i As Integer
For i = 10 To 1 Step -1
Debug.Print i
' pause for 1 second
Sleep 1000
DoEvents
Next i
End Sub
0
 
LVL 1

Expert Comment

by:hmadhur
ID: 8217995
Hi
Try using Sleep API. Type this statement in a VB module.

Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

Then inbetween some line in your loop, add this function with the no of milliseconds delay you want. For eg. If you call this function :

Sleep 1000

you will get a delay of 1 second. And so on.

Hope it helps.

Regards
Madhur
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:List244
ID: 8218238
You could do as they have said with the sleep api but i would recommend using another loop so like

For FIRSTLOOP = 1 to 10
'Your normal code here
    for PAUSELOOP = 1 to 10000
         DoEvents
    Next PAUSELOOP
next FIRSTLOOP


If you add a pause loop like that, unlike SleepApi it will not freeze your program while 'sleeping' to raise/lower speed with this raise/lower the '10000' to get it to the speed you would like
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 8218436
So you think calling DoEvents 10,000 Times is better than using the sleep api?
0
 

Expert Comment

by:ricapp
ID: 8222005
' Why not do it the easy way ?
' Thats whats VB for, isnt it ?
'
' Add a windows Form
' Add one Timer control (name it Timer1)
' Add one Button control (name it Command1)
' Add one Lable control (name it Label1)
' Paste this code, press the button !

Private m_lngCounter As Long
Private Const S_NUM As Long = 10
Private Const E_NUM As Long = 0

Private Sub Command1_Click()
   
    Timer1.Interval = 1000
    Timer1.Enabled = True
    m_lngCounter = S_NUM
    'm_lngCounter
End Sub

Private Sub Timer1_Timer()
   
    m_lngCounter = m_lngCounter - 1
    Label1.Caption = m_lngCounter
    If m_lngCounter = E_NUM Then
        Timer1.Enabled = False
    End If
End Sub

' Good luck !
0
 
LVL 8

Expert Comment

by:List244
ID: 8222185
Timers are horrible, the timing is off, truthfully i never use them anymore.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question