Link to home
Start Free TrialLog in
Avatar of JoeD77
JoeD77

asked on

Sleeping in VB6

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
Avatar of JoeD77
JoeD77

ASKER

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?

Avatar of Mike Tomlinson
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
Avatar of JoeD77

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JoeD77

ASKER

brilliant ^.^ thanks