我是vb.net多线程的新手。
如果我做错了什么,请纠正我。
我想要做的是:
用户需要把序列号放在文本框中,然后程序需要从供应商网站上挑选型号和保修信息,当用户点击搜索按钮时。
我的windows窗体有两个textbox和两个one浏览器,然后是一个datagridview控件。
我尝试做的是当用户点击搜索时,代码需要做以下事情
Thread1:从textbox1选择序列号,需要使用webbrowser1线程从web获取信息2:从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‘,因为当前线程不在单线程单元中。
如果你告诉我哪里做错了,需要做什么等等,那就太好了
谢谢Sathish
发布于 2013-05-10 00:47:11
在创建线程时,请尝试设置单元状态。
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()https://stackoverflow.com/questions/16465917
复制相似问题