Solved

Delaying a For / Next LOOP

Posted on 2002-06-10
17
220 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
  • 4
  • 3
  • 3
  • +5
17 Comments
 
LVL 2

Expert Comment

by:vbDoc
Comment Utility
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
Comment Utility
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
Comment Utility
Don't understand. Is this a VB program? You are talking about a Browser?
0
 

Author Comment

by:bs0d
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
'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
Comment Utility
Yes use the sleep API
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:bs0d
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Or, just add the word Private before the API declaration

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

Expert Comment

by:DanRollins
Comment Utility
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
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now