Solved

Delaying a For / Next LOOP

Posted on 2002-06-10
17
225 Views
Last Modified: 2010-05-02
I have a For / Next LOOP that I'd wish to SLOW DOWN. I have it opening a page in the browser and it just works too fast for the browser to handle. SO, i figure that if I can slow it down, IE will have more time to process the loop. Can anybody help?

I've talked to a few, but they really dont know VB all too well, and know that generally a "sleep" function is available in other languages. I've thought of using a "time" but really havent messsed much with any "time stuff".

Thanks in advance.

-bs0d

0
Comment
Question by:bs0d
[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
  • 4
  • 3
  • 3
  • +5
17 Comments
 
LVL 2

Expert Comment

by:vbDoc
ID: 7068954
You can use time. Very easy. This loop will wait for two seconds and cont. on.

Dim dt as date
dt = DateAdd("s", 2, Now)

Do Until dt < Now
    DoEvents
Loop
0
 

Author Comment

by:bs0d
ID: 7068956
One thing though, I need it to work fast, but not that fast that the browser cannot handle. Can I get that to work slower, like 1/2 sec? or 1/4 of a sec.?
0
 
LVL 2

Expert Comment

by:vbDoc
ID: 7068968
Don't understand. Is this a VB program? You are talking about a Browser?
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:bs0d
ID: 7068973
LOL, I have a VB Program that is opening a URL when a cmd button is clicked. Multiple URL's will be opened and I need them to be opened quickly, but not so fast that the browser isnt even understanding it, and just opens the last one processed; So I need to slow the loop down.
0
 
LVL 2

Expert Comment

by:vbDoc
ID: 7068989
Ok, If you need to have a small time like 1/4 - 1/2 sec then use a timer control. The value is in MillSeconds (1000 = 1 second). Easy to use. Good example with the help file.

Doesn't the internet control reports back when it has opened a web page?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 7069042
You can also use the SLEEP API.  This will stop your application code for whatever time you want.  This is similar to the Timer control, which is itself built upon the TIMER API (callback).

Since your question had to do with slowing a For...Next loop, this is the method.  If you want to replace the For...Next with a Timer control/API, that is a different approach to the problem you are facing.
0
 
LVL 1

Expert Comment

by:LJunior
ID: 7069211
'place this code in one of your modules:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'========================================================
'usage:
sleep 1000 'will sleep 1 second
0
 

Expert Comment

by:Recca
ID: 7069346
Yes use the sleep API
0
 

Author Comment

by:bs0d
ID: 7071524
Just to confirm guys; in "General Declarations" I would declare :

Declare Sub Sleep Lib "kernel32" (byVal dwMilliseconds as Long)
----------------------

Then in my For / Next Loop i'd use it like this:

(example....)
'
For X = 0 to 5
  lblAns = 1 + X
     Sleep 1000      ' 1 second.
Next X
'

If I want it to be .5 / sec. then the # would be 500 ?

0
 
LVL 45

Expert Comment

by:aikimark
ID: 7071776
Correct.  The Sleep API, the Timer control, and the Timer API all use time in milliseconds.  It is an unsigned integer with a max value of 64K (65535).
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7076475
When you say that your program opens up multiple URLs, how are you opening them?  Are you using ShellExecute to run multiple instances of IE?  Or, does your application use the Internet Control?

If you are using the WebBrowser1 control, then there is a property called Busy that will be true as long as the browser is still busy trying to navigate to the url.  You can write code like this:

WebBrowser1.Navigate "www.microsoft.com"
While WebBrowser1.Busy
    DoEvents
Wend
0
 
LVL 45

Expert Comment

by:aikimark
ID: 7076634
mdougan,

1. It is probably better to use the Do...Loop, since M$ has stated that its lifetime is limited.  There is a While...End While in VB.Net

2. If you want the rest of your system and other applications to perform better, it would be better to incorporate a call to the SLEEP API in the loop that checks for the WebBrowser1.Busy condition to become true.  Although your program is constantly handing control back to Windows to handle messages and run other processes, your program is busier than it needs to be and will take cycles away from other processes with the DoEvents loop you've suggested.
0
 

Author Comment

by:bs0d
ID: 7077170
COMPILE ERROR:
  Constants, fixed-length strings, arrays, user defined types and
declare statments not allowed as public members of object modules.
------------------------------------

I got this error when I inserted this code in my loop.
'
'
this code:
gen declarations:
'
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
-------------------
loop:
'
sleep 1000
=================================
also, i am opening the browser like this:
gen. declarations:
'
Private Const SW_SHOWNORMAL = 1
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
  "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As _
  String, ByVal lpFile As String, ByVal lpParameters As String, _
  ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'
-----------------------

'
then in my loop:
'
  For X = 0 To Val(txtAmount)
        Name2 = Name0 + cboNamez.List(X)
        Call ShellExecute(Me.hwnd, "open", URL, "", App.Path, SW_SHOWNORMAL)
          Sleep 1000
    Next X
'
0
 
LVL 1

Accepted Solution

by:
LJunior earned 50 total points
ID: 7077547
place the declaration of the sleep command in the gen declarations of a M O D U L E not a form
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7077889
Or, just add the word Private before the API declaration

Private Declare Sub Sleep.....
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7899915
Hi bs0d,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept LJunior's comment(s) as an answer.

bs0d, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Expert Comment

by:SpideyMod
ID: 7978278
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

717 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