Toh
Toh

Reputation: 99

WebBrowser DocumentComplete Not Loaded on First Click VB.Net

I have a problem when click button 1st time, webbrowser.DocumentText always not loaded, but on 2nd or 3rd time clickings the documentText is ALWAYS loaded.

Glad if someone can advice.

my code as below:

Private Property pageready As Boolean = False

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button3.Click
        WebBrowser1.Navigate(url)
        WaitForPageLoad()
        RichTextBox1.Text = WebBrowser1.DocumentText
        Msgbox ("Document Loaded")
    End Sub


    Private Sub WaitForPageLoad()
        AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
        While Not pageready
            Application.DoEvents()
        End While
        pageready = False
    End Sub

    Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
        If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
            pageready = True
            RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
        End If
    End Sub

Upvotes: 0

Views: 1024

Answers (2)

Visual Vincent
Visual Vincent

Reputation: 18320

Here are two different ways you can dynamically wait for the WebBrowser to load the entire page:

  1. Extending your Async/Await method to actually wait for the page to load you can use a ManualResetEvent. The MRE functions as a door: When you call Set() you open the door, when you call Reset() you close it, and when you call WaitOne(-1) you wait by the door for it to open (if it's closed).

    Private DocumentCompletedResetEvent As New ManualResetEvent(False)
    
    Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button3.Click
        WebBrowser1.Navigate(url)
        Await ExampleMethodAsync()
        RichTextBox1.Text = WebBrowser1.DocumentText
        Msgbox("Document Loaded")
    End Sub
    
    Async Function ExampleMethodAsync() As Task
        DocumentCompletedResetEvent.Reset() 'Makes sure that the MRE is closed.
        DocumentCompletedResetEvent.WaitOne(-1) 'Waits for it to open.
    End Function
    
    Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
            DocumentCompletedResetEvent.Set() 'Opens the MRE.
        End If
    End Sub
    
  2. The second method adds a temporary event handler to the DocumentCompleted event. Using Lambda expressions you may create an in-line method for the event handler.

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(TextBox1.Text)
    
        'Creates a temporary event handler.
        Dim DocumentCompletedHandler As WebBrowserDocumentCompletedEventHandler = _
            Sub()
                If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
                    RemoveHandler WebBrowser1.DocumentCompleted, DocumentCompletedHandler 'Removes the temporary event handler.
                    RichTextBox1.Text = WebBrowser1.DocumentText
                    Msgbox("Document Loaded")
                End If
            End Sub
        AddHandler WebBrowser1.DocumentCompleted, DocumentCompletedHandler 'Adds the temporary event handler.
    End Sub
    

Upvotes: 0

Toh
Toh

Reputation: 99

Task Delay till the DocumentComplete loaded is the solustion for me.

Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button3.Click
    WebBrowser1.Navigate(url)
    Await ExampleMethodAsync()
    RichTextBox1.Text = WebBrowser1.DocumentText
    Msgbox ("Document Loaded")
End Sub

Async Function ExampleMethodAsync() As Task
    ' The following line simulates a task-returning asynchronous process.  
    Await Task.Delay(1750)
End Function

Upvotes: 1

Related Questions