首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用VB.net的多线程抛出错误

使用VB.net的多线程抛出错误
EN

Stack Overflow用户
提问于 2013-05-09 23:52:32
回答 1查看 1.5K关注 0票数 0

我是vb.net多线程的新手。

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

我想要做的是:

用户需要把序列号放在文本框中,然后程序需要从供应商网站上挑选型号和保修信息,当用户点击搜索按钮时。

我的windows窗体有两个textbox和两个one浏览器,然后是一个datagridview控件。

我尝试做的是当用户点击搜索时,代码需要做以下事情

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

我的代码看起来像这样

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-10 00:47:11

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

代码语言:javascript
复制
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()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16465917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档