Multithreading Help

G

Guest

I trying to learn how multithread works. I found an example in C# and I am
trying to make it work in VB, but I am having problems with it. Could
someone help me to make it work. Here is the code I am working with. Why is
the main thread executed first and them the other?

Imports System.Threading
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim thr As Thread

thr = New Thread(AddressOf ProcessMsg)
thr.Start()
Call MainThread()
End Sub

Private Delegate Sub WriteMsg(ByVal value As String)

Private Sub PostMsg(ByVal value As String)
Me.TextBox1.Text += "Secondary Thread: " & value & vbNewLine
End Sub

Private Sub ProcessMsg()
Dim i As Long

For i = 0 To 5
Me.Invoke(New WriteMsg(AddressOf PostMsg), New Object()
{i.ToString})
Next
End Sub

Private Sub MainThread()
Dim i As Long

For i = 0 To 5
Me.TextBox1.Text += "Main Thread: " & i.ToString & vbNewLine
Me.TextBox1.Refresh()
Thread.Sleep(350)
Next
End Sub
End Class
 
J

Jon Skeet [C# MVP]

HLong said:
I trying to learn how multithread works. I found an example in C# and I am
trying to make it work in VB, but I am having problems with it. Could
someone help me to make it work. Here is the code I am working with. Why is
the main thread executed first and them the other?

Your worker thread is using Control.Invoke to call back to the main
thread. That's exactly the right thing to do, but in this case you've
kept the main thread busy doing other things first. The calls to Invoke
will only be made once the UI thread has finished processing
Button1_Click.

If you manually made calls to Application.DoEvents I suspect you'd see
the secondary thread in action earlier - but DoEvents is a generally
bad idea.
 
G

Guest

Thank you.
I am just starting to learn about multithreading. Does this mean that the
main thread has priority over any other thread? I have tried setting the
priority of the second thread to highest and still did not make any diff. If
I start two threads from the control, then both are executed, how I would
expect. What would be the best approach to execute code simultaneously from
a form? TIA
 
J

Jon Skeet [C# MVP]

HLong said:
I am just starting to learn about multithreading. Does this mean that the
main thread has priority over any other thread?

No - although GUI threads are a high priority. The problem is using
Control.Invoke. Your secondary thread is getting as far as the call to
Invoke, but then it's got to wait for the UI to finish before it can
get any further.
I have tried setting the
priority of the second thread to highest and still did not make any diff. If
I start two threads from the control, then both are executed, how I would
expect. What would be the best approach to execute code simultaneously from
a form? TIA

Don't put *either* of the tasks in the main GUI thread - the UI thread
should remain responsive at all times, if possible. Start two different
threads to do the work, and you'll see both executing.
 
D

Doker

Jon Skeet [C# MVP] pisze:
No - although GUI threads are a high priority. The problem is using
Control.Invoke. Your secondary thread is getting as far as the call to
Invoke, but then it's got to wait for the UI to finish before it can
get any further.
What you are trying to say is that Invoke method adds a message to
message queue and waits till it's executed, and the execution of the
queue is in the GUI thread which is busy, right?
So doing DoEvents from time to time in the GUI thread would solve the
problem.
 
G

Guest

Thank you very much.

Jon Skeet said:
No - although GUI threads are a high priority. The problem is using
Control.Invoke. Your secondary thread is getting as far as the call to
Invoke, but then it's got to wait for the UI to finish before it can
get any further.


Don't put *either* of the tasks in the main GUI thread - the UI thread
should remain responsive at all times, if possible. Start two different
threads to do the work, and you'll see both executing.
 
J

Jon Skeet [C# MVP]

Doker said:
Jon Skeet [C# MVP] pisze:
No - although GUI threads are a high priority. The problem is using
Control.Invoke. Your secondary thread is getting as far as the call to
Invoke, but then it's got to wait for the UI to finish before it can
get any further.
What you are trying to say is that Invoke method adds a message to
message queue and waits till it's executed, and the execution of the
queue is in the GUI thread which is busy, right?

Yes, that's right.
So doing DoEvents from time to time in the GUI thread would solve the
problem.

Yes, but in a nasty way which could lead to re-entrancy issues.
DoEvents should be avoided where possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top