Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在新线程vb.net中创建WebBrowser时出错_.net_Vb.net_Multithreading_Visual Studio_Visual Studio 2010 - Fatal编程技术网

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

在新线程vb.net中创建WebBrowser时出错,.net,vb.net,multithreading,visual-studio,visual-studio-2010,.net,Vb.net,Multithreading,Visual Studio,Visual Studio 2010,我正在尝试使用新线程创建一个新的webbrowser,但当我尝试运行该程序时,总是会出现以下错误: 有人能帮我纠正这个错误吗?这是我的密码: 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(

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

有人能帮我纠正这个错误吗?这是我的密码:

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)中。根据错误,只能在单线程单元(STA,例如UI线程)中的线程上创建
WebBrowser
控件

不建议更改线程单元,因为它必须是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.
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
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.
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
制作方法:

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.
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
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.
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

针对.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.
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
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.
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
制作方法:

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.
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
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.
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

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

不建议更改线程单元,因为它必须是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.
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
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.
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
制作方法:

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.
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
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.
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

针对.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.
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
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.
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
制作方法:

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.
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
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.
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

错误文本的图像通常是一个坏主意,因为人们想要在错误文本上进行搜索错误文本的图像通常是一个坏主意,因为人们想要在错误文本上进行搜索