Events between threads?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,


I hvae worker class to manage the application
inside worker : worker creates (n) number of threads to do IO operations.

The Problem Is:
to handle all threads : [create new] , [process finished] , [clean all]
I have to keep worker process inside while statement:
while (true)
...
and it takes 50% of cpu all time!
I'm wondering how can I use events to keep worker sleep until one of the
opetations threads finish?
NOTE: event must wake worker thread with event args.
AND NOTE : worker thread must be diffrent thread than IO threads.

Thank u
[ i'm using : C# 2005 beta 2 ]
 
I have to keep worker process inside while statement:
while (true)
..
and it takes 50% of cpu all time!

just call Thread.Sleep(...) inside the loop. this should help.
Wiktor Zychla
 
is this the way e.g. windows waits for process to finish?
or events is the way?

What about ThreadPool, does it do the trick?
 
is this the way e.g. windows waits for process to finish?

no, Thread.Sleep is not the way to wait for the worker thread to finish.

if the only purpose of the while () {...} loop is to wait for worker threads
then use the System.Threading.WaitHandle.WaitAll() method.
read the docs for further info on this method.

Wiktor Zychla
 
Thanks,

I have unlimited number of processes [broadcast]
so i need to create very dynamic ThreadPool,
When ever any thread finish a job , Main Thread has to take the result, then
inserts new thread , and so on,
But as i see in this example:
http://msdn2.microsoft.com/library/9f7e54k1(en-us,vs.80).aspx

1- array of WaitHandle must be decalred. Which very bad for my problem
2- I want to wait for each [ NOT FOR ALL, NOT FOR ANY] thread to finish to
process its result and create new thread.

I Hope u can help me further.
Thanks agian.
 
I have unlimited number of processes [broadcast]
so i need to create very dynamic ThreadPool,
When ever any thread finish a job , Main Thread has to take the result,
then
inserts new thread , and so on,
But as i see in this example:
http://msdn2.microsoft.com/library/9f7e54k1(en-us,vs.80).aspx

1- array of WaitHandle must be decalred. Which very bad for my problem
2- I want to wait for each [ NOT FOR ALL, NOT FOR ANY] thread to finish
to
process its result and create new thread.

I Hope u can help me further.
Thanks agian.

I see two possible routes you could try:
a) use one of thread synchronization objects, a mutex or an event to sync
the threads
b) use async delegates with async notifications (delegate.BeginInvoke( new
AsyncCallback( notification_routine ), ... ).

The latter solution could be an option when async operations should return
values for further processing. By using async delegates, you avoid dealing
with threads at all.

Wiktor Zychla
 
Thanks Wiktor

But what i know , using asynch and beginInvoke raise event in different
(new) thread, not the main thread, is that right?
How to overcome this problem?
thanks

Wiktor Zychla said:
I have unlimited number of processes [broadcast]
so i need to create very dynamic ThreadPool,
When ever any thread finish a job , Main Thread has to take the result,
then
inserts new thread , and so on,
But as i see in this example:
http://msdn2.microsoft.com/library/9f7e54k1(en-us,vs.80).aspx

1- array of WaitHandle must be decalred. Which very bad for my problem
2- I want to wait for each [ NOT FOR ALL, NOT FOR ANY] thread to finish
to
process its result and create new thread.

I Hope u can help me further.
Thanks agian.

I see two possible routes you could try:
a) use one of thread synchronization objects, a mutex or an event to sync
the threads
b) use async delegates with async notifications (delegate.BeginInvoke( new
AsyncCallback( notification_routine ), ... ).

The latter solution could be an option when async operations should return
values for further processing. By using async delegates, you avoid dealing
with threads at all.

Wiktor Zychla
 
Thanks Wiktor

But what i know , using asynch and beginInvoke raise event in different
(new) thread, not the main thread, is that right?
How to overcome this problem?
thanks
 
Back
Top