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