473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Threading in .Net...

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...

Jun 6 '06 #1
4 1196
On 6 Jun 2006 12:12:57 -0700, CirclesTraveled wrote:
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.
Jun 6 '06 #2
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

"CirclesTraveled" <hi****@ecg-inc.com> schreef in bericht
news:11**********************@j55g2000cwa.googlegr oups.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...

Jun 6 '06 #3

CirclesTraveled wrote:
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.

--
Larry Lard
Replies to group please

Jun 7 '06 #4
Thank you all for you suggestions. I will be exploring all
possibilities. I will let everyone know what happens... Again, Thank
you...

Jun 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
2
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
6
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service...
2
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True...
11
by: Paul Sijben | last post by:
I am stumped by the following problem. I have a large multi-threaded server accepting communications on one UDP port (chosen for its supposed speed). I have been profiling the code and found...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
0
by: kingcrowbar.list | last post by:
Hello Everyone I have been playing a little with pyGTK and threading to come up with simple alert dialog which plays a sound in the background. The need for threading came when in the first...
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.