Hi, I'm trying to find a way to make backgroundworker do what's in the webbrowser.
There is a time-consuming part in the webbrowserdocumentcompleted event that I want backgroundworker to tackle. Unfortunately, I don't know how to link that part to backgroundworker so that BW can do that job instead of webbrowser.

Here's what in the webbrowserdocumentcompleted event:

Sub WebBrowser1DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            If CheckBox1.Checked = True Then
                'Create a new instance of the Decaptcher class
                Dim decap As Decaptcher = New Decaptcher(TextBox3.Text, TextBox4.Text)

                Dim strSource As String = ""

                For Each Captcha As HtmlElement In WebBrowser1.Document.Images
                    If Captcha.GetAttribute("src").Contains("http://www.google.com/recaptcha/api/image?c=03AHJ") Then
                        strSource = Captcha.GetAttribute("src")
                        Exit For
                    End If

                'Downloads the image
                Dim requestPic As WebRequest = WebRequest.Create(strSource)
                Dim responsePic As WebResponse = requestPic.GetResponse()
                Dim img As Image = Image.FromStream(responsePic.GetResponseStream())

                Dim answer As String = decap.sendCaptcha(img)

                Dim badCaptcha As Boolean = False
                If badCaptcha = True Then
                End If

                WebBrowser1.Document.GetElementById("recaptcha_response_field").SetAttribute("value", answer)
                For i As Integer = 0 To WebBrowser1.Document.All.Count - 1
                    If WebBrowser1.Document.All(i).GetAttribute("type").Contains("submit") Then
                    End If
            End If

            If CheckBox1.Checked = False Then
                For Each Captcha As HtmlElement In WebBrowser1.Document.All
                    If Captcha.GetAttribute("src").Contains("http://www.google.com/recaptcha/api/image?c=03AHJ") Then
                        CaptchaForm.picBoxCaptchaImage.ImageLocation = Captcha.GetAttribute("src")
                    End If
            End If

        End If

The time consuming part starts from line 2 to line 30. My program freezes whenever webbrowser tries to do this job, so I need BW to do this specific job instead. Any way to accomplish that?

5 Years
Discussion Span
Last Post by serph09

WebBrowser uses Internet Explorer which is a COM component. COM components have a threading model, IE uses STA (Single Threaded Apartment). Which is another way of saying "it doesn't support threading". You are allowed to make a method call in a BGW but COM will automatically marshal the call to the UI thread. Since all method calls and property accesses actually happen on the UI thread, you will make it a lot slower by using a BGW.

You can in fact run WebBrowser on another thread. You will have to create a thread that is STA. That requires calling Thread.SetApartmentState() to switch to STA and Application.Run() to pump a message loop. The latter is hard requirement, but also makes it very difficult to use the WB. You'll need to use Control.BeginInvoke() to run methods on that thread.

Consider using the WebRequest class instead.

From here: http://stackoverflow.com/questions/2491236/backgroundworker-and-webbrowser-control


Thanks for the reply.
That seems complicated for me to accomplish at this point:p
Any other way of doing this?

Edited by serph09: n/a


I tried processing that time-consuming process in another thread but the program just crashed and got the common "Send/Don't Error" report to MS.
Here's what I did.
I put all of those codes into..Private Sub BackgroundProcess()

and within my WB, when I need that time-consuming code to be used, I do this:

Dim t As Thread
                t = New Thread(AddressOf Me.BackgroundProcess)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.