Multi-Threaded App

  • Thread starter Thread starter askhuy
  • Start date Start date
A

askhuy

Hi All

Would someone please explain to me why my threads are not running at the
sametime? Here is what I don't understand. When I invoke the TestThread()
procedure, I always get the output of main thread first and then the
secondary thread's output. Shouldn't the ouput show the mixed messages



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
TestThread()
End Sub



Private Sub TestThread()
'Create new thread and define its starting point
Dim t As New Thread(New ThreadStart(AddressOf DoSomeTask))
'run the new thread
t.Start()

' print some messages to screen
Dim i As Integer
For i = 1 To 1000
TextBox1.Text &= "from main thread # = " & i.ToString & vbCrLf
Next
End Sub

Sub DoSomeTask()
Dim i As Integer

For i = 1 To 1000
TextBox1.Text &= "from secondary thread # = " & i.ToString &
vbCrLf
Next
End Sub


Here are my outputs

from main thread # = 1
from main thread # = 2
from main thread # = 3
from main thread # = 4
from main thread # = 5
from main thread # = 6
from main thread # = 7
from main thread # = 8
from main thread # = 9
from main thread # = 10
from secondary thread # = 1
from secondary thread # = 2
from secondary thread # = 3
from secondary thread # = 4
from secondary thread # = 5
from secondary thread # = 6
from secondary thread # = 7
from secondary thread # = 8
from secondary thread # = 9
from secondary thread # = 10
 
No not in this case ,, wich is also bad coding practice for multithreading

1. ever heard of thread switching and thread prio`s ?
2. do you know that GUI controls run on the main thread ?
and dealing from sperate threads should invoke a synchronization
mechanism ?

You probably wrote this in .Net 2003 ?? as in .Net 2005 it should not be
possible to run this code

about point 1 ,,, you also probably run this on a SP system and not on a MP
system ,, however you could force a thread switch by coding some thread
sleeps in the 2 procedures . but as i said the shown code is a practicce to
show how MT should not be implemented


regards

Michel Posseth [MCP]
 
askhuy said:
Hi All

Would someone please explain to me why my threads are not running at
the sametime? Here is what I don't understand. When I invoke the
TestThread() procedure, I always get the output of main thread first and
then the secondary thread's output. Shouldn't the ouput show the mixed
messages



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
TestThread()
End Sub



Private Sub TestThread()
'Create new thread and define its starting point
Dim t As New Thread(New ThreadStart(AddressOf DoSomeTask))
'run the new thread
t.Start()

' print some messages to screen
Dim i As Integer
For i = 1 To 1000
TextBox1.Text &= "from main thread # = " & i.ToString & vbCrLf
Next
End Sub

Sub DoSomeTask()
Dim i As Integer

For i = 1 To 1000
TextBox1.Text &= "from secondary thread # = " & i.ToString &
vbCrLf
Next
End Sub


Here are my outputs

from main thread # = 1
from main thread # = 2
from main thread # = 3
from main thread # = 4
from main thread # = 5
from main thread # = 6
from main thread # = 7
from main thread # = 8
from main thread # = 9
from main thread # = 10
from secondary thread # = 1
from secondary thread # = 2
from secondary thread # = 3
from secondary thread # = 4
from secondary thread # = 5
from secondary thread # = 6
from secondary thread # = 7
from secondary thread # = 8
from secondary thread # = 9
from secondary thread # = 10
In your TestThread you have "from main thread # = "
Then under DoSomeTask you have "from secondary thread # = "

Isn't that a reversal of meaning?

Galen
 
Hi guys,

Thanks for your helps. I tried with the console windows and both threads
execute at the same time.

Regards

Sam



Michel Posseth said:
No not in this case ,, wich is also bad coding practice for multithreading

1. ever heard of thread switching and thread prio`s ?
2. do you know that GUI controls run on the main thread ?
and dealing from sperate threads should invoke a synchronization
mechanism ?

You probably wrote this in .Net 2003 ?? as in .Net 2005 it should not be
possible to run this code

about point 1 ,,, you also probably run this on a SP system and not on a
MP system ,, however you could force a thread switch by coding some thread
sleeps in the 2 procedures . but as i said the shown code is a practicce
to show how MT should not be implemented


regards

Michel Posseth [MCP]








askhuy said:
Hi All

Would someone please explain to me why my threads are not running at
the sametime? Here is what I don't understand. When I invoke the
TestThread() procedure, I always get the output of main thread first and
then the secondary thread's output. Shouldn't the ouput show the mixed
messages



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
TestThread()
End Sub



Private Sub TestThread()
'Create new thread and define its starting point
Dim t As New Thread(New ThreadStart(AddressOf DoSomeTask))
'run the new thread
t.Start()

' print some messages to screen
Dim i As Integer
For i = 1 To 1000
TextBox1.Text &= "from main thread # = " & i.ToString & vbCrLf
Next
End Sub

Sub DoSomeTask()
Dim i As Integer

For i = 1 To 1000
TextBox1.Text &= "from secondary thread # = " & i.ToString &
vbCrLf
Next
End Sub


Here are my outputs

from main thread # = 1
from main thread # = 2
from main thread # = 3
from main thread # = 4
from main thread # = 5
from main thread # = 6
from main thread # = 7
from main thread # = 8
from main thread # = 9
from main thread # = 10
from secondary thread # = 1
from secondary thread # = 2
from secondary thread # = 3
from secondary thread # = 4
from secondary thread # = 5
from secondary thread # = 6
from secondary thread # = 7
from secondary thread # = 8
from secondary thread # = 9
from secondary thread # = 10
 
askhuy said:
Would someone please explain to me why my threads are not running at the
sametime? Here is what I don't understand. When I invoke the TestThread()
procedure, I always get the output of main thread first and then the
secondary thread's output. Shouldn't the ouput show the mixed messages

They /will/ run at the same time, provided each allows the other a look
in from time to time.
Add a call to ...

System.Threading.Thread.Sleep( 0 )

.... into each loop - this tells the thread to yield to other processes
(i.e. Threads). Without this, each Thread will "hog" the CPU, doing its
own thing until it's finished.

HTH,
Phill W.
 
Back
Top