0

I have a Private Sub Fill(), which im trying to call from button1, in the form of

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.

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>&lt;.*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 
2
  • 1
    are you aware of the GUI thread/worker thread marshalling requirement? Commented Jan 11, 2010 at 4:20
  • Here's a little tutorial on multi-threading in VB.Net: chrishaas.wordpress.com/2009/06/25/… Commented Jan 11, 2010 at 15:14

3 Answers 3

3

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 BeginInvoke and a delegate as in the example below:

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 
Sign up to request clarification or add additional context in comments.

4 Comments

But the OP is complaining that the Fill function never kicks off, not that it does not do what's required.
Not complaining about an exception either. Probably uses Form1.TextBox1.Text. That doesn't work, Form1 creates a new form. An invisible one.
yes, nobugz is right. I don't get an exception, or anything else. I just click the button and nothing happens. I have included the Fill() function in my original post. Please have a look, and offer any suggestions to get it working with multithreading. Thanks, for your efforts.
I‘ve just plugged your Fill() method into my mock up and it worked fine! Here are some things that I would do to try to pin this down: Comment all the code out inside Fill(). Does Fill() get called from the multi threading call? If not then the problem lies in the Fill() code. So, I would try to pin down the problem line of code by uncommenting each line of code and testing. If Fill() does get called then something else on the form must be getting in the way. I’d strip the form down to pin this down – removing controls and code that are not needed in Fill().
0

Start by placing a breakpoint inside the Fill method. I bet it starts up just fine.

1 Comment

When I call the Fill method, by commenting out the multithreading code, and just putting Fill() instead, it works. That's why I'm confused as to why it doesnt work with MT...
-1

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".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.