Raise event inside a thread

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

Guest

Hi,
I hope u can help me

I asked this question before but I didn't explain it very well.

Let say I hvae a thread 'A', creates object 'O', and start thread 'B' to do
some work on 'O', OK?
I want thread 'B' to fire event of 'O'
BUT MUST THAT EVENT DONE BY THREAD 'A' not 'B'!!

I want the formal way, not a tricky one!

How can I do this?
[ any help, samples, links, keyword to serach will be appricated ]

plesase , i have a projct depends on this!

Waleed
 
Waleed AlRashoud said:
I hope u can help me

I asked this question before but I didn't explain it very well.

Let say I hvae a thread 'A', creates object 'O', and start thread 'B' to do
some work on 'O', OK?
I want thread 'B' to fire event of 'O'
BUT MUST THAT EVENT DONE BY THREAD 'A' not 'B'!!

I want the formal way, not a tricky one!

How can I do this?
[ any help, samples, links, keyword to serach will be appricated ]

plesase , i have a projct depends on this!

Well, thread A will have to be waiting for work to do, which you can do
with a producer/consumer queue as shown half way down
http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml

Then just make the event handler add the appropriate work item to the
queue, and make thread A the consumer of the queue.
 
Thanks Jon,

it's full answer for my qustion , but I still couldn't explain it very well!

Again, let us make a new situation:
Thread 'A' is the main thread , will create Thread 'B' to do some work on
Object o, then thread 'A' should continu create threads 'C','D','...',.., and
so on, with max concurrent 10 for example, each thread do the smae job [ for
seprate object : oB,oC,oD,oE,..];
each thread finish its job,will raise event to be catched from thread 'A'..
to create a new object 'oJ' and create new thread 'J' to do some work. and so
on
What I'm trying to avoid is :
while(true){}
to check for each thread is't done or not, I don't think while(true) is a
good way!
...
I have seen something called ISynchronize.invoke, But i think it's working
only with UI-thread . is there any similar interface or something in non-UI
threads?
I hope so!

Thanks again
 
Waleed AlRashoud said:
it's full answer for my qustion , but I still couldn't explain it very well!

Again, let us make a new situation:
Thread 'A' is the main thread , will create Thread 'B' to do some work on
Object o, then thread 'A' should continu create threads 'C','D','...',.., and
so on, with max concurrent 10 for example, each thread do the smae job [ for
seprate object : oB,oC,oD,oE,..];
each thread finish its job,will raise event to be catched from thread 'A'..
to create a new object 'oJ' and create new thread 'J' to do some work. and so
on
What I'm trying to avoid is :
while(true){}
to check for each thread is't done or not, I don't think while(true) is a
good way!
..
I have seen something called ISynchronize.invoke, But i think it's working
only with UI-thread . is there any similar interface or something in non-UI
threads?
I hope so!

If that's the only reason you need to effectively raise the event in
thread A, there's a much better approach - thread pooling. I've got a
sample custom thread pool in my miscutil library:
http://www.pobox.com/~skeet/csharp/miscutil

Otherwise you could make thread A wait on a monitor, and notify that
monitor each time a thread completes. You might potentially want to
have a counter which each thread increments (safely) to say how many
threads have finished, and which thread A decrements when it creates a
new thread. Effectively at that stage you've got a counting semaphore.
 
Thanks Jon, good work with ur lib..!

I tested ur work with this addition, to chcek if event is cathed by main
thread [ as 'A' ] or in other new thread [ as 'X' ].
in [Test.cs] , I added these lines inside [static void TestThreadPool() ]
/// Start
pool.AfterWorkItem += new AfterWorkItmeHandler(Pool_After);
Console.WriteLine("Main Thread ID " +
Thread.currentThread.ManagedThreadId.ToString();
/// End

Then I created new static void method [Pool_After(... ,...) ] and wrote:
///Start
Console.WriteLine("Event Catched By " +
Thread.currentThread.ManagedThreadId.ToString());
///End

This is Output:
/// Start Output
Main Thread ID 9
Event Catched By 11
/// End output

So! event not catched by main Thread!
Is it a mistake I did using CustomThreadPool , Or threads not raiseing
events inside main thread?
 
Waleed AlRashoud said:
I tested ur work with this addition, to chcek if event is cathed by main
thread [ as 'A' ] or in other new thread [ as 'X' ].
in [Test.cs] , I added these lines inside [static void TestThreadPool() ]
/// Start
pool.AfterWorkItem += new AfterWorkItmeHandler(Pool_After);
Console.WriteLine("Main Thread ID " +
Thread.currentThread.ManagedThreadId.ToString();
/// End

Then I created new static void method [Pool_After(... ,...) ] and wrote:
///Start
Console.WriteLine("Event Catched By " +
Thread.currentThread.ManagedThreadId.ToString());
///End

This is Output:
/// Start Output
Main Thread ID 9
Event Catched By 11
/// End output

So! event not catched by main Thread!
No.

Is it a mistake I did using CustomThreadPool , Or threads not raiseing
events inside main thread?

No, they can't be. The point was that you can signal to the main thread
within the event, using monitors or a ManualResetEvent.
 
Back
Top