How to make Thread1 executed before Thread2 ?

  • Thread starter Thread starter fniles
  • Start date Start date
F

fniles

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
EachMessageThread = New
Threading.Thread(AddressOf clsEachMessage.ProcessMessage)
EachMessageThread.Name = "Thread " &
threadCount.ToString()
EachMessageThread.Start()
Thread.Sleep(0)
 
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
EachMessageThread = New
Threading.Thread(AddressOf clsEachMessage.ProcessMessage)
EachMessageThread.Name = "Thread " &
threadCount.ToString()
EachMessageThread.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
 
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
 
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
 
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

Cool - I didn't know that! Thanks Mattias!

Thanks,

Seth Rowe
 
fniles said:
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
EachMessageThread = New
Threading.Thread(AddressOf clsEachMessage.ProcessMessage)
EachMessageThread.Name = "Thread " &
threadCount.ToString()
EachMessageThread.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.
 
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 ?

Perhaps you shouldn't be spwaning threads for each process. Maybe submit
requests to a queue, then process the queue with a single thread to
guarantee order.
 
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
EachMessageThread = New
Threading.Thread(AddressOf clsEachMessage.ProcessMessage)
EachMessageThread.Name = "Thread " &
threadCount.ToString()
EachMessageThread.Start()
Thread.Sleep(0)

The ideal pattern to use here is a blocking queue. A blocking queue
is just like a normal queue except that the dequeue method blocks
until something gets enqueued from another thread. The dequeueing
thread typically cycles through an infinite loop dequeueing items and
processing them in the order they arrive. It's an incredibly simply
pattern to implement if you can get your hands on a solid (truly
thread-safe) implementation of a blocking queue.
 
Back
Top