Multi-Threaded App

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • askhuy

    #1

    Multi-Threaded App

    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(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    TestThread()
    End Sub



    Private Sub TestThread()
    'Create new thread and define its starting point
    Dim t As New Thread(New ThreadStart(Add ressOf 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




  • Michel Posseth  [MCP]

    #2
    Re: Multi-Threaded App

    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" <askhuy@yahoo.c omschreef in bericht
    news:%23oxAq7ys GHA.1876@TK2MSF TNGP06.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(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    TestThread()
    End Sub
    >
    >
    >
    Private Sub TestThread()
    'Create new thread and define its starting point
    Dim t As New Thread(New ThreadStart(Add ressOf 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
    >
    >
    >
    >

    Comment

    • Chris

      #3
      Re: Multi-Threaded App


      If you are using VB 2005 have a look at
      http://msdn.microsoft.com/library/de...nginvb2005.asp
      for an awesome article that goes over the BackgroundWorke r. There is even
      sample code.

      Chris

      "askhuy" <askhuy@yahoo.c omwrote in message
      news:%23oxAq7ys GHA.1876@TK2MSF TNGP06.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(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button1.Click
      TestThread()
      End Sub
      >
      >
      >
      Private Sub TestThread()
      'Create new thread and define its starting point
      Dim t As New Thread(New ThreadStart(Add ressOf 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
      >
      >
      >
      >

      Comment

      • Galen Somerville

        #4
        Re: Multi-Threaded App


        "askhuy" <askhuy@yahoo.c omwrote in message
        news:%23oxAq7ys GHA.1876@TK2MSF TNGP06.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(B yVal sender As System.Object, ByVal e As
        System.EventArg s) Handles Button1.Click
        TestThread()
        End Sub
        >
        >
        >
        Private Sub TestThread()
        'Create new thread and define its starting point
        Dim t As New Thread(New ThreadStart(Add ressOf 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



        Comment

        • askhuy

          #5
          Re: Multi-Threaded App

          Hi guys,

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

          Regards

          Sam



          "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
          news:uV1%23hR0s GHA.4420@TK2MSF TNGP04.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" <askhuy@yahoo.c omschreef in bericht
          news:%23oxAq7ys GHA.1876@TK2MSF TNGP06.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(B yVal sender As System.Object, ByVal e As
          >System.EventAr gs) Handles Button1.Click
          > TestThread()
          > End Sub
          >>
          >>
          >>
          > Private Sub TestThread()
          > 'Create new thread and define its starting point
          > Dim t As New Thread(New ThreadStart(Add ressOf 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
          >>
          >>
          >>
          >>
          >
          >

          Comment

          • Phill W.

            #6
            Re: Multi-Threaded App

            askhuy wrote:
            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.Threadin g.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.

            Comment

            Working...