Thread question

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

Guest

I have a main worker thread that spawns a child thread to do some listening.
What I would like to have happen is to have the child thread raise events in
the parent thread, and have the parent thread respond to those events.

Is it possible to have one thread raise events in another thread? So far I
have not been able to do so. Each thread seems to only be able to raise it's
own events.

Thanks in advance for any help.
 
Stirling said:
I have a main worker thread that spawns a child thread to do some listening.
What I would like to have happen is to have the child thread raise events in
the parent thread, and have the parent thread respond to those events.

Is it possible to have one thread raise events in another thread? So far I
have not been able to do so. Each thread seems to only be able to raise it's
own events.

Yup - raising events is effectively just calling methods.

You'll need your worker thread to be running some kind of message pump
or queue - it won't be able to do other things and suddenly be
"interrupted" with an event.

For an example of such a queue, see
http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml
 
Is there any kind of event in the parent thread that gets raised when a
message enters the queue? How does the thread know when another thread has
entered something into the queue?
 
Stirling said:
Is there any kind of event in the parent thread that gets raised when a
message enters the queue? How does the thread know when another thread has
entered something into the queue?

The way the example I mentioned is coded, the parent thread wakes up
from its call to Monitor.Wait, because there's a call to Monitor.Pulse
when a message enters the queue.
 
Stirling said:
I have a main worker thread that spawns a child thread to do some listening.
What I would like to have happen is to have the child thread raise events in
the parent thread, and have the parent thread respond to those events.

Is it possible to have one thread raise events in another thread? So far I
have not been able to do so. Each thread seems to only be able to raise it's
own events.

Are you sure that it's necessary that the event be handled on the parent's
thread? It's probably OK to let the event be handed on the child thread.
You just have to watch out for UI control access and sync issues. It
depends on what you are really needing to do.

One other problem you will have with the queue technique is that the child
thread can pump those messages in pretty fast. The way I describe is
naturally throttled because the child thread can't raise another event until
the earlier event handler finishes. YMMV.

-- Alan
 
Back
Top