tags:

views:

122

answers:

2

Creates a webbrowser in a background thread, then is supposed to navigate to a url and return a htmldocument, but the navigate fails for some reason, any idea why?

NOTE: this is part of a class where you see ME.url or me. ... me.isrunning = boolean false by default me.url = uri of location to navigate to

 Public Function GetDOC(ByVal url As Uri) As HtmlDocument
    If Me.IsRunning Then
        Throw New Exception("Object Currently In use")
    End If
    htmdoc = Nothing
    Dim cp As New Threading.Thread(AddressOf thrdowo)
    cp.SetApartmentState(Threading.ApartmentState.STA)
    cp.Start()
    run = True

    While run

    End While
    Return htmdoc

End Function

Private Sub thrdowo()
    Dim cbl As New WebBrowser
    'cbl.Url = Me.URL
    cbl.Navigate("about:blank")
    cbl.Navigate(Me.URL)

    While cbl.IsBusy

    End While
    htmdoc = cbl.Document


    run = False
End Sub
+2  A: 

Use WebRequest rather than WebBrowser

You can change your implementation to use WebRequest.BeginGetResponse which will fetch your resource asynchronously without having to manage the threads yourself.

Gurdas Nijor
+1  A: 

I am not expert with the Webbrowser control, but I just went through the same thing two weeks ago. The problem is that when you tell the Webbrowser to do something (like Navigate, or click a button), it has to go off and do its work including probably, waiting for websites to respond to its requests. This all happens asynchronously however, so hammering it multiple times with Navigate calls will almost certainly fail.

What you need to do is to give it work (Navigate, etc.) and then exit your current thread, and wait for the Webbrowser to finish what you asked it to do, and then tell you by raising an event, then you can go to the next step.

So what I do is to have one object that maintains a state, and depending on the current state, it does something to the Webbrowser, advances the state, and then exits the thread.

I have another object (Form actually) that receives all of the events from the Webbrowser, determines when an event means the it is "done" loading and then re-calls the first object again.

RBarryYoung