|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Multi-Threaded AppWould 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] Show quoteHide quote "askhuy" <ask***@yahoo.com> schreef in bericht news:%23oxAq7ysGHA.1876@TK2MSFTNGP06.phx.gbl... > 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 > > > > Hi guys,
Thanks for your helps. I tried with the console windows and both threads execute at the same time. Regards Sam Show quoteHide quote "Michel Posseth [MCP]" <M***@posseth.com> wrote in message news:uV1%23hR0sGHA.4420@TK2MSFTNGP04.phx.gbl... > 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" <ask***@yahoo.com> schreef in bericht > news:%23oxAq7ysGHA.1876@TK2MSFTNGP06.phx.gbl... >> 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 >> >> >> >> > > If you are using VB 2005 have a look at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/threadinginvb2005.asp for an awesome article that goes over the BackgroundWorker. There is even sample code. Chris Show quoteHide quote "askhuy" <ask***@yahoo.com> wrote in message news:%23oxAq7ysGHA.1876@TK2MSFTNGP06.phx.gbl... > 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 > > > >
Show quote
Hide quote
"askhuy" <ask***@yahoo.com> wrote in message In your TestThread you have "from main thread # = "news:%23oxAq7ysGHA.1876@TK2MSFTNGP06.phx.gbl... > 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 > > Then under DoSomeTask you have "from secondary thread # = " Isn't that a reversal of meaning? Galen askhuy wrote:
> Would someone please explain to me why my threads are not running at the They /will/ run at the same time, provided each allows the other a look > 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 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.
Check result of call into Windows API
Listview columnheader question Print ID card using Plastic Card Printer in VB.net Date and Time Calculation. Regex doesn't match when test string is in middle of file Winforms User COntrol Addhandler in a Multithreading class and the events raised in seperate threads, how to get to the ca How Do I Create an Entry Point for a DLL? blue mask over icon How to toggle the RTS/DTS line |
|||||||||||||||||||||||