【问题标题】:Multithreading using VB.net Throws error使用 VB.net 的多线程抛出错误
【发布时间】:2013-05-09 15:52:32
【问题描述】:

vb.net 多线程新手。

如果我做错了什么,请纠正我。

我想做什么:

用户需要将序列号输入文本框,然后程序需要在用户点击搜索按钮时从供应商网站选择型号和保修信息。

我的窗体有两个文本框和两个网络浏览器,然后是一个 datagridview 控件。

我想要做的是当用户点击搜索按钮时代码需要在下面做的事情

Thread1:从 textbox1 中选择序列号,需要使用 webbrowser1 从 web 获取信息 Thread2:从textbox2中选择序列号,需要使用webbrowser2从web获取信息

我的代码看起来像

Dim thread1 As System.Threading.Thread
Dim thread2 As System.Threading.Thread

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)          Handles Button1.Click

    thread1 = New System.Threading.Thread(AddressOf thread11)
    thread1.Start()

    thread2 = New System.Threading.Thread(AddressOf thread22)
    thread2.Start()

   End Sub

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles       MyBase.Load
    Me.CheckForIllegalCrossThreadCalls = False
End Sub

 Sub thread11()
 ' Takes the serial no from text1 and searches the information using webbrowser1 goes here
end sub 

Sub thread22()
 ' Takes the serial no from text2 and searches the information using webbrowser2 goes here
  end sub 

但我在调试代码时收到以下错误消息

创建表单时出错。有关详细信息,请参阅 Exception.InnerException。错误是:ActiveX 控件 '8856f961-340a-11d0-a96b-00c04fd705a2' 无法实例化,因为当前线程不在单线程单元中。

如果你能说出我做错了什么、需要做什么以及所有这些,那就太好了

谢谢 萨蒂什

【问题讨论】:

标签: vb.net multithreading winforms


【解决方案1】:

创建线程时,尝试设置单元状态。

thread1 = New System.Threading.Thread(AddressOf thread11)
thread1.SetApartmentState(ApartmentState.STA)
thread1.Start()

thread2 = New System.Threading.Thread(AddressOf thread22)
thread2.SetApartmentState(ApartmentState.STA);
thread2.Start()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多