Threading in .Net...

  • Thread starter CirclesTraveled
  • Start date
C

CirclesTraveled

Hello Everyone,

I have to write a program that uses multiple threads. Simply, this is
what I want to do.

1) The parent thread will spawn at least two child threads.
2) Child Thread 1 will monitor data from a source and if the
conditions are TRUE, it would post a message to the top of a message
queue. Then alert Child Thread 2 that there is a message in the queue.
3) Child Thread 2 would pop the tail of the message queue until all
the messages have been processed and reported to the user.

Normally in C++ or Delphi, I would use a Critical Section variables
around the global memory structure that would prevent more than one
thread accessing the data in memory at the same time and use windows
postmessage events to notify the thread that it needs to look at the
message queue. Unfortunately, I can not use C++, Dephi or C#. I have
to use VB .Net. The good thing about .Net is the fact that it supports
threading. It is very easy to create threads and start them in .Net.
However, the way it provides messaging between threads is a little
confusing to me or I might be just over analysing it. I could use
monitor like the critical section variables to protect the global
memory structure, but how I do I notify Child Thread 2 that there is a
message waiting in the Queue.

What is the recomended way to do this in .Net?

Thank you in advance...
 
M

Mehdi

It is very easy to create threads and start them in .Net.
However, the way it provides messaging between threads is a little
confusing to me or I might be just over analysing it. I could use
monitor like the critical section variables to protect the global
memory structure, but how I do I notify Child Thread 2 that there is a
message waiting in the Queue.

What is the recomended way to do this in .Net?

Use a ManualResetEvent. Have thread 2 wait on it. In thread 1, when a new
item is placed on the queue, set the event. This will Cause thread 2 to
wake up. Then thread 2 can do it's job and process all the items in the
queue. When it's done it can reset the ManualResetEvent and wait until
woken up again.
 
C

Cor Ligthert [MVP]

Circles,

I would use two threads.
The parent thread and the worker thread.

I assume that you are using the queue class.

You can thrown an event in the workerthread to tell that you have placed
something in the queue.

Another approach and even simpler is just a timerevent in the mainthread in
what you look if the queue is empty or not, and if not, just start
processing it until it is empty.

Be aware that you synclock the queue before you something put in (from the
worker thread) or get from (from the mainthread) it.

Don't forget to look in the queue (if that is needed) at closing from the
program.

I hope this helps,

Cor
 
L

Larry Lard

CirclesTraveled said:
Hello Everyone,

I have to write a program that uses multiple threads. Simply, this is
what I want to do.

1) The parent thread will spawn at least two child threads.
2) Child Thread 1 will monitor data from a source and if the
conditions are TRUE, it would post a message to the top of a message
queue. Then alert Child Thread 2 that there is a message in the queue.
3) Child Thread 2 would pop the tail of the message queue until all
the messages have been processed and reported to the user. ....
What is the recomended way to do this in .Net?

This is known as a 'producer-consumer queue', and I know of at least
two implementations published on the web:

Jon Skeet's:
<http://www.yoda.arachsys.com/csharp/threads/deadlocks.shtml> as part
of his excellent pages on threading in general. In C#, but you should
be able to understand it.

Joe Albahari's: <http://www.albahari.com/threading/index.html>, in the
'Wait and Pulse' section. Also in C#, but with pretty colors too! :)

Since it sounds like you know how to do this stuff in C++, there
shouldn't be any major surprises awaiting you in the .NET threading
model.
 
C

CirclesTraveled

Thank you all for you suggestions. I will be exploring all
possibilities. I will let everyone know what happens... Again, Thank
you...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Threading..... 4
Threading question.... 4
Threading 1
basic threading question 2
.NET EVents & Threading 4
Threading Question - Newbie 3
Threading so restricted? 4
Threading question 1

Top