?
Solved

Slowing down a loop

Posted on 2003-03-27
7
Medium Priority
?
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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!

 
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

Technology Partners: 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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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 Month12 days, 10 hours left to enroll

777 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