• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Delaying a For / Next LOOP

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
bs0d
Asked:
bs0d
  • 4
  • 3
  • 3
  • +5
1 Solution
 
vbDocCommented:
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
 
bs0dAuthor Commented:
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
 
vbDocCommented:
Don't understand. Is this a VB program? You are talking about a Browser?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
bs0dAuthor Commented:
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
 
vbDocCommented:
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
 
aikimarkCommented:
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
 
LJuniorCommented:
'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
 
ReccaCommented:
Yes use the sleep API
0
 
bs0dAuthor Commented:
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
 
aikimarkCommented:
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
 
mdouganCommented:
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
 
aikimarkCommented:
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
 
bs0dAuthor Commented:
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
 
LJuniorCommented:
place the declaration of the sleep command in the gen declarations of a M O D U L E not a form
0
 
mdouganCommented:
Or, just add the word Private before the API declaration

Private Declare Sub Sleep.....
0
 
DanRollinsCommented:
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
 
SpideyModCommented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 3
  • 3
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now