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

Getting an error when creating a WebBrowser in a new thread vb.net

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

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

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

非常感谢您的帮助,现在已经卡在这个错误上好几天了。

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

不建议更改线程的单元,因为它必须是 MTA 才能与 UI 线程一起进行多线程处理。

这几乎只剩下一个选择:仅在 UI 线程上创建 和修改 控件,并在你的新线程中完成其他繁重的工作。

我将在 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