使用VB.net执行多线程时抛出错误
我是vb.net多线程的新手 如果我做错了什么,就责备我 我想做的是: 用户需要将序列号输入文本框,然后当用户点击搜索按钮时,程序需要从供应商网站上选择型号和保修信息 我的windows窗体有两个textbox和两个WebBrowser,然后是一个datagridview控件 我想做的是当用户点击搜索按钮时,代码需要做以下事情 Thread1:从textbox1中选择序列号,并需要使用webbrowser1从web获取信息 Thread2:从textbox2中选择序列号,并需要使用webbrowser2从web获取信息 我的代码看起来像使用VB.net执行多线程时抛出错误,vb.net,multithreading,winforms,Vb.net,Multithreading,Winforms,我是vb.net多线程的新手 如果我做错了什么,就责备我 我想做的是: 用户需要将序列号输入文本框,然后当用户点击搜索按钮时,程序需要从供应商网站上选择型号和保修信息 我的windows窗体有两个textbox和两个WebBrowser,然后是一个datagridview控件 我想做的是当用户点击搜索按钮时,代码需要做以下事情 Thread1:从textbox1中选择序列号,并需要使用webbrowser1从web获取信息 Thread2:从textbox2中选择序列号,并需要使用webbrow
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”,因为当前线程不在单线程单元中
如果你告诉我我做错了什么,需要做什么,以及所有这些,那就太好了
谢谢
Sathish创建线程时,请尝试设置单元状态
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()
可能重复的,通常使用除主程序线程以外的任何其他线程与UI控件交互都是不好的做法。尝试使用BackgroundWorker,因为它使这类事情更安全,难度更小。