在新线程vb.net中创建WebBrowser时出错

时间:2016-07-16 01:36:11

标签: .net vb.net multithreading visual-studio visual-studio-2010

我正在尝试使用新线程创建一个新的webbrowser,但是当我尝试运行该程序时,我总是收到以下错误:

enter image description here 有人可以帮我解决这个错误吗?这是我的代码:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click


    Dim curFile1 As String = TextBox4.Text
    If (File.Exists(curFile1)) Then
        MsgBox("Ok")
    Else
        MsgBox("Please a User Agent file", MsgBoxStyle.Critical, "Error")
        Exit Sub
    End If

    Dim curFile2 As String = TextBox1.Text
    If (File.Exists(curFile2)) Then
        MsgBox("Ok")
    Else
        MsgBox("Please a IP file", MsgBoxStyle.Critical, "Error")
        Exit Sub
    End If

    Try
        ' Open the file using a stream reader.
        Using sr As New StreamReader(curFile2)
            ' Read the stream to a string and write the string to the console.
            ip_text = File.ReadAllLines(curFile2)
        End Using
    Catch er As Exception
        MsgBox("The file could not be read: IP", MsgBoxStyle.Critical, "Error")
        Exit Sub
    End Try


    Try
        ' Open the file using a stream reader.
        Using sr2 As New StreamReader(curFile1)
            ' Read the stream to a string and write the string to the console.
            user_text = File.ReadAllLines(curFile1)
        End Using
    Catch er As Exception
        MsgBox("The file could not be read: User Agent", MsgBoxStyle.Critical, "Error")
        Exit Sub
    End Try


    nr = 0
    For Each ip As String In ip_text

        MsgBox(ip)

        trd = New Thread(AddressOf make_it)
        trd.IsBackground = True
        trd.Start()

        nr = nr + 1
    Next


End Sub

Private Sub make_it()
    Dim decible = New WebBrowser()
    web_panel.Controls.Add(decible)

    decible.Name = "browser" & nr
    Dim RandomIpAddress As String = user_text(GetRandom(user_text.Length))
    MsgBox(RandomIpAddress)

    ChangeUserAgent(RandomIpAddress)
    decible.Navigate("http://www.whatsmyuseragent.com/")

End Sub

非常感谢帮助,暂时坚持这个错误。

1 个答案:

答案 0 :(得分:2)

您收到错误,因为您创建的新线程通常位于所谓的 Multithreaded Apartment (MTA)中。根据错误,WebBrowser控件只能在单线程单元中的线程上创建(STA,例如UI线程)。

不建议更改线程的公寓,因为它必须是MTA才能在UI线程旁边进行多线程处理。

这使您只有一个选项:仅在UI线程上创建 并修改 控件,并在新线程中执行另一个繁重的工作。< / p>

我会在UI线程上创建控件,在线程的最后我将执行调用以运行导航代码。

.NET Framework 4.0或更高版本的解决方案:

在你的循环中:

Dim decible = New WebBrowser()
decible.Name = "browser" & nr
web_panel.Controls.Add(decible)

trd = New Thread(AddressOf make_it)
trd.IsBackground = True
trd.Start(decible) 'Passing the web browser as a parameter.

make_it方法:

Private Sub make_it(ByVal decible As WebBrowser)
    Dim RandomIpAddress As String = user_text(GetRandom(user_text.Length))
    MsgBox(RandomIpAddress)

    ChangeUserAgent(RandomIpAddress)

    If Me.InvokeRequired = True Then
        Me.Invoke(Sub() decible.Navigate("http://www.whatsmyuseragent.com/"))
    Else
        decible.Navigate("http://www.whatsmyuseragent.com/")
    End If

End Sub

.NET Framework 3.5或更低版本的解决方案:

在你的循环中(这与上面相同):

Dim decible = New WebBrowser()
decible.Name = "browser" & nr
web_panel.Controls.Add(decible)

trd = New Thread(AddressOf make_it)
trd.IsBackground = True
trd.Start(decible) 'Passing the web browser as a parameter.

make_it方法:

Private Sub make_it(ByVal decible As WebBrowser)
    Dim RandomIpAddress As String = user_text(GetRandom(user_text.Length))
    MsgBox(RandomIpAddress)

    ChangeUserAgent(RandomIpAddress)

    NavigateWebBrowser(decible, "http://www.whatsmyuseragent.com/")

End Sub

Delegate Sub NavigateDelegate(ByVal Browser As WebBrowser, ByVal Url As String)

Private Sub NavigateWebBrowser(ByVal Browser As WebBrowser, ByVal Url As String)
    If Me.InvokeRequired = True Then
        Me.Invoke(New NavigateDelegate(AddressOf NavigateWebBrowser), Browser, Url)
    Else
        Browser.Navigate(Url)
    End If
End Sub