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