How to make Thread1 executed before Thread2 ?

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

    How to make Thread1 executed before Thread2 ?

    I am using VB.NET 2005. My program receives messages, and for every message
    I create a thread.
    Say I received Message 1, spawn the thread, then receive Message 2, spawn
    the thread.
    Even though I spawn the Message1 thread before Message2 thread, sometimes
    Message 2 thread is executed before Message 1 thread.
    Is there a way for me make sure Thread1 is executed before Thread2 ?

    Thank you.

    newmessage$ = "....."
    Dim clsEachMessage As New EachMessage
    clsEachMessage. Packet = newmessage
    If threadCount 100 Then threadCount =
    0
    threadCount += 1
    clsEachMessage. threadCount = threadCount
    EachMessageThre ad = New
    Threading.Threa d(AddressOf clsEachMessage. ProcessMessage)
    EachMessageThre ad.Name = "Thread " &
    threadCount.ToS tring()
    EachMessageThre ad.Start()
    Thread.Sleep(0)


  • rowe_newsgroups

    #2
    Re: How to make Thread1 executed before Thread2 ?

    On Sep 14, 1:07 pm, "fniles" <fni...@pfmail. comwrote:
    I am using VB.NET 2005. My program receives messages, and for every message
    I create a thread.
    Say I received Message 1, spawn the thread, then receive Message 2, spawn
    the thread.
    Even though I spawn the Message1 thread before Message2 thread, sometimes
    Message 2 thread is executed before Message 1 thread.
    Is there a way for me make sure Thread1 is executed before Thread2 ?
    >
    Thank you.
    >
    newmessage$ = "....."
    Dim clsEachMessage As New EachMessage
    clsEachMessage. Packet = newmessage
    If threadCount 100 Then threadCount =
    0
    threadCount += 1
    clsEachMessage. threadCount = threadCount
    EachMessageThre ad = New
    Threading.Threa d(AddressOf clsEachMessage. ProcessMessage)
    EachMessageThre ad.Name = "Thread " &
    threadCount.ToS tring()
    EachMessageThre ad.Start()
    Thread.Sleep(0)
    By executed do you mean the method started or the method finished?

    You could always use the Join method of the thread, but that seems to
    defeat the purpose of threads. For the most part, threads should not
    be affected by sequence, they should be able to run fine no matter
    which runs first.

    Thanks,

    Seth Rowe

    Comment

    • rowe_newsgroups

      #3
      Re: How to make Thread1 executed before Thread2 ?

      Thread.Sleep(0)

      Sorry, I forgot to ask you what the purpose of this was?

      Unless I'm missing something, isn't all this is doing is wasting a few
      nano-seconds to run an unnecessary cpu cycle?

      Thanks,

      Seth Rowe


      Comment

      • Mattias Sjögren

        #4
        Re: How to make Thread1 executed before Thread2 ?

        >Thread.Sleep(0 )
        >
        >Sorry, I forgot to ask you what the purpose of this was?
        >
        >Unless I'm missing something, isn't all this is doing is wasting a few
        >nano-seconds to run an unnecessary cpu cycle?
        From the Thread.Sleep docs:

        "The number of milliseconds for which the thread is blocked. Specify
        zero (0) to indicate that this thread should be suspended to allow
        other waiting threads to execute."

        So it's basically a way to yield to other threads without actually
        putting your own thread to sleep.

        Not sure if it actually makes sense for it to be used in the original
        posters code though. It doesn't look meaningful in that context.


        Mattias

        --
        Mattias Sjögren [C# MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        • rowe_newsgroups

          #5
          Re: How to make Thread1 executed before Thread2 ?

          On Sep 14, 3:09 pm, Mattias Sjögren <mattias.dont.w ant.s...@mvps.o rg>
          wrote:
          Thread.Sleep(0)
          >
          Sorry, I forgot to ask you what the purpose of this was?
          >
          Unless I'm missing something, isn't all this is doing is wasting a few
          nano-seconds to run an unnecessary cpu cycle?
          >
          From the Thread.Sleep docs:
          >
          "The number of milliseconds for which the thread is blocked. Specify
          zero (0) to indicate that this thread should be suspended to allow
          other waiting threads to execute."
          >
          So it's basically a way to yield to other threads without actually
          putting your own thread to sleep.
          >
          Not sure if it actually makes sense for it to be used in the original
          posters code though. It doesn't look meaningful in that context.
          >
          Mattias
          >
          --
          Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.ne t/dotnet/|http://www.dotnetinterop.com
          Please reply only to the newsgroup.
          Cool - I didn't know that! Thanks Mattias!

          Thanks,

          Seth Rowe

          Comment

          • =?ISO-8859-1?Q?G=F6ran_Andersson?=

            #6
            Re: How to make Thread1 executed before Thread2 ?

            fniles wrote:
            I am using VB.NET 2005. My program receives messages, and for every message
            I create a thread.
            Say I received Message 1, spawn the thread, then receive Message 2, spawn
            the thread.
            Even though I spawn the Message1 thread before Message2 thread, sometimes
            Message 2 thread is executed before Message 1 thread.
            Is there a way for me make sure Thread1 is executed before Thread2 ?
            >
            Thank you.
            >
            newmessage$ = "....."
            Dim clsEachMessage As New EachMessage
            clsEachMessage. Packet = newmessage
            If threadCount 100 Then threadCount =
            0
            threadCount += 1
            clsEachMessage. threadCount = threadCount
            EachMessageThre ad = New
            Threading.Threa d(AddressOf clsEachMessage. ProcessMessage)
            EachMessageThre ad.Name = "Thread " &
            threadCount.ToS tring()
            EachMessageThre ad.Start()
            Thread.Sleep(0)
            >
            >
            If you want the messages to be handled in sequence, there is no reason
            to handle them in separate threads.

            Put the messages in a queue when they arrive, and let a single thread
            process the queue.

            --
            Göran Andersson
            _____
            Göran Anderssons privata hemsida.

            Comment

            Working...