Sleeping in VB6

JoeD77
JoeD77 used Ask the Experts™
on
Hi.
I'm trying to cycle through a text file, and using each line in that text file to launch a webpage (using WebBrowser.Navigate(TextOfLine)... The problem is, Sleep is not working properly, so the pages are cycled faster than they can load. I need each page to stay up for about 2 seconds before continuing.

Here's the code:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Load()
Form1.Show
LaunchLineByLine

End Sub
Public Sub LaunchLineByLine()
Open "hosts.txt" For Input As #1
Line Input #1, a
'The very first line
MsgBox a
Do Until EOF(1)
Line Input #1, a
'subsequent lines
WebBrowser1.Navigate a
Sleep 2000
Loop
Close #1
End Sub

Open in new window


I know that Sleep is the problem because when I replace "Sleep 2000" with MsgBox a" (and hence breakpointing that loop execution), the page loads nicely as it should.

I need to be able to do this using Sleep, however.

Thanks in advance
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Sorry, after some debugging, it seems like Sleep is actually halting the execution of the program, but the webpage I'm launching is not loading whilst sleeping. Does anyone know how to permit the loading of the webpage while sleeping?

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Use a loop and only Sleep() for a very short duration.  Use DoEvents() to keep the app responsive:

    Private Sub Delay(seconds As Long)
        Dim endTime As Date
   
        endTime = DateAdd("s", seconds, Now())
        Do While Now() < endTime
            Sleep 50
            DoEvents
        Loop
    End Sub

Usage:

    Delay 2 ' two second delay

Author

Commented:
Thank you.

Changed code to:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Load()
Form1.Show
LaunchLineByLine

End Sub
Public Sub LaunchLineByLine()
Open "hosts.txt" For Input As #1
Line Input #1, a
'The very first line
MsgBox a
Do Until EOF(1)
Line Input #1, a
'subsequent lines
Delay (a)
Loop
Close #1
End Sub
    Private Sub Delay(page As String)
        Dim endTime As Date
    
        endTime = DateAdd("s", 5, Now())
        Do While Now() < endTime
        If Now() >= endTime Then
            DoEvents
            LaunchPage (page)
       End If
        Loop
    End Sub
Public Sub LaunchPage(page As String)
WebBrowser1.Navigate (page)
End Sub

Open in new window


now it will show the first webpage, the 3rd webpage, and the last webpage.. (assuming I'm using a list of ~6 webpages)... so it's still very inconsistent... is there any way to improve the efficiency of this?

Thanks for your reply
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Don't LaunchPage() in the middle of the Delay()...

*Change back to my original Delay() method*

And then something like:

    Do Until EOF(1)
        Line Input #1, a
        'subsequent lines
        LaunchPage a
        Delay 5
    Loop

Author

Commented:
brilliant ^.^ thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial