|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Threading in .Net...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... On 6 Jun 2006 12:12:57 -0700, CirclesTraveled wrote:
> It is very easy to create threads and start them in .Net. Use a ManualResetEvent. Have thread 2 wait on it. In thread 1, when a new> 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? 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. 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 Show quoteHide quote "CirclesTraveled" <hic***@ecg-inc.com> schreef in bericht news:1149621177.500865.273710@j55g2000cwa.googlegroups.com... > 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... > CirclesTraveled wrote:
> Hello Everyone, This is known as a 'producer-consumer queue', and I know of at least> > 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? 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. -- Larry Lard Replies to group please Thank you all for you suggestions. I will be exploring all
possibilities. I will let everyone know what happens... Again, Thank you...
Why Me? (Instead of Form1)
How do I increment a byte with out casting? Capturing mouse events (Mouse up and down on the desktop) Loading CrystalReports.rpt, I Am Asked To Enter Login ID And Password DAAB problem most natural behavior on mouse wheel Cancel Constructor (Me = Nothing) Parent child binding question RichTextBox in Vs2005 Vb.Net shows unformatted RTF Rollover button sampel |
|||||||||||||||||||||||