【问题标题】:Getting an error when creating a WebBrowser in a new thread vb.net在新线程 vb.net 中创建 WebBrowser 时出错
【发布时间】:2016-07-16 01:36:11
【问题描述】:

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

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

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

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

【问题讨论】:

  • 错误文本的图像通常不是一个好主意,因为人们会想要对错误文本进行搜索

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


【解决方案1】:

您收到错误是因为您创建的新线程通常位于所谓的多线程单元 (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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 2012-01-28
    • 2021-12-07
    • 1970-01-01
    相关资源
    最近更新 更多