Question

I want to Limit Waiting for Webbrowser to Load; I started with the code below that is waiting for Webbrowser to load before starting next action; I want the waiting to be limited to 60 seconds, if the webpage does not load then the code will perform the next action; Any help is appreciated:

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    WebBrowser1.Navigate("www.mekdam.com")
    While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
        Application.DoEvents()
    End While
End Sub
End Class

Thanks

Was it helpful?

Solution

You should check your ReadyState in the DocumentCompleted event. That way it won't hang your application.

If you add a timer you can cancel the loading of the page if it takes too long:

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
    WebBrowser1.Navigate("http://www.msn.co.uk")
    Timer1.Interval = 2000
    Timer1.Enabled = True
End Sub

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    'If the timer is not running then we are not waiting for the document to complete
    If Not Timer1.Enabled Then Exit Sub

    'Check the document that just loaded is the main document page (not a frame) and that the control is in the ready state
    If e.Url.AbsolutePath = CType(sender, WebBrowser).Url.AbsolutePath AndAlso WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        'we loaded the page before the timer elapsed
        Timer1.Enabled = False
    End If
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'timer has elapsed so cancel page load
    WebBrowser1.Stop()
    'Disable the timer
    Timer1.Enabled = False
End Sub

OTHER TIPS

Dim secondTime As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    WebBrowser1.Navigate("http://www.msn.co.uk")
    Timer1.Interval = 100
    Timer1.Enabled = True
    secondTime = False
    Timer1.Start()
End Sub

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If e.Url.AbsolutePath = CType(sender, WebBrowser).Url.AbsolutePath AndAlso WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        'loading completed before the timer elapsed
        Timer1.Enabled = False
        Timer1.Stop()
    End If
End Sub


Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'timer we cancle the loading as it takes long time
    If secondTime = True Then
        WebBrowser1.Stop()  'Second time call
    Else
        secondTime = True   'First time call
    End If
End Sub

Synopsis

Where the object Timer1 is set to Enabled or Time1.Start() it calls the Timer1.Tick function. So the page will be prevented to load at the start. To prevent this unwanted stopping in loading before the time escape we use a variable secondTime that ensures the call is made second time or the given time is escaped. To confirm that we use IF comparison and make it ready to get the abort call on the second time call or after the given interval. The condition inside the Timer1.Tick ensures that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top