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(TextOf Line)... 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:
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
I'm trying to cycle through a text file, and using each line in that text file to launch a webpage (using WebBrowser.Navigate(TextOf
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
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
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
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
ASKER
Thank you.
Changed code to:
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
brilliant ^.^ thanks
ASKER