VB.net 简单多线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2039873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.net Simple Multithreading
提问by Rudy
I have a Private Sub Fill(), which im trying to call from button1, in the form of
我有一个 Private Sub Fill(),我试图从 button1 调用它,形式为
Dim t1 As System.Threading.Thread = New System.Threading.Thread(AddressOf Me.Fill)
t1.Start()
However, when I run the program nothing happens. I click the button numerous times and the function isnt being executed. What gives? The Fill function is basically a outputting bunch of html from IE into a textbox, running regex and outputting the results in a listbox.
但是,当我运行程序时,什么也没有发生。我多次单击该按钮,但该功能并未执行。是什么赋予了?Fill 函数基本上是将 IE 中的一堆 html 输出到文本框中,运行正则表达式并将结果输出到列表框中。
Can anyone help me get this working? I'd appreciate the help. EDIT:Below, is the Fill function that I am trying to get working. The function itself works, when i try it without multithreading. But not with it...
谁能帮我解决这个问题?我很感激你的帮助。 编辑:下面是我正在尝试使用的填充功能。当我在没有多线程的情况下尝试时,该函数本身可以工作。但不是用它...
Private Sub Fill()
Try
For Each links In ListBox2.Items
Dim blah As Boolean = False
Do While blah = False
Application.DoEvents()
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
blah = True
WebBrowser1.Navigate(links)
Application.DoEvents()
Me.Refresh()
'OUTPUT THE REGEX IN RTB
Try
RichTextBox1.Text = WebBrowser1.Document.Body.OuterHtml
RichTextBox1.Update()
Application.DoEvents()
Me.Refresh()
'INTRODUCE REGEX
If CheckBox1.Checked = True Then
Dim R As New Regex("</H3><.*gt;")
For Each M As Match In R.Matches(RichTextBox1.Text)
Dim email As String = M.Value.Substring(9).Split("&;").GetValue(0).ToString
ListBox1.Items.Add(email)
Next
End If
Catch ex As Exception
Label1.Text = "Error recieved. Program will not stop"
Me.Refresh()
End Try
Application.DoEvents()
Me.Refresh()
End If
Loop
Next
Catch ex As Exception
End Try
End Sub
回答by Carl Rippon
I think you are having problems because you are not on the UI thread when you are trying to write to the textbox in the Fill() method – this will causes an exception. To solve the problem you need to switch to the UI thread using BeginInvokeand a delegate as in the example below:
我认为您遇到了问题,因为当您尝试在 Fill() 方法中写入文本框时,您不在 UI 线程上 – 这将导致异常。要解决此问题,您需要使用BeginInvoke和委托切换到 UI 线程,如下例所示:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t1 As System.Threading.Thread = New System.Threading.Thread(AddressOf Me.Fill)
t1.Start()
End Sub
Private Delegate Sub FillDelegate()
Private Sub Fill()
If TextBox1.InvokeRequired Then
TextBox1.BeginInvoke(New FillDelegate(AddressOf Fill))
Else
TextBox1.Text = "Worked!!!!"
End If
End Sub
回答by AngryHacker
Start by placing a breakpoint inside the Fill method. I bet it starts up just fine.
首先在 Fill 方法中放置一个断点。我敢打赌它启动得很好。
回答by Idle_Mind
Make sure your Button handler still has Handles Button1.Click
on the end of it. Sometimes people cut and paste the Button to somewhere else on their form and this causes the IDE to un-wire it and leave the handler "orphaned".
确保您的 Button 处理程序仍然Handles Button1.Click
在它的末尾。有时,人们将 Button 剪切并粘贴到表单上的其他位置,这会导致 IDE 取消连接它并使处理程序“孤立”。