firing events between threads

  • Thread starter Thread starter Bob Lazarchik
  • Start date Start date
B

Bob Lazarchik

Hello:

I need to fire events between two threads. Any suggestions on the best way
to accomplish this. Scenerio:

Thread A is main thread. Thread B is a Com port listener that fires an
event when it receives input. I need to fire this event to Thread A yet
allow thread B to continue processing while thread A handles the event. I
have looked at using the Messaging classes but this looks like overkill to
accomplish this.

Any suggestions are greatly appreciated.

Thanks

Bob
 
Bob Lazarchik said:
I need to fire events between two threads. Any suggestions on the best way
to accomplish this. Scenerio:

Thread A is main thread. Thread B is a Com port listener that fires an
event when it receives input. I need to fire this event to Thread A yet
allow thread B to continue processing while thread A handles the event. I
have looked at using the Messaging classes but this looks like overkill to
accomplish this.

Any suggestions are greatly appreciated.

When you say Thread A is the "main" thread, what exactly do you mean?
What else is it doing? Unless it's running some sort of message pump
(or the like - a queue of jobs or whatever) then you're out of luck -
you can't just interrupt a thread and tell it to do something
different.

You could use a different thread entirely, of course, or the
threadpool.
 
Hi Bob,

If you are working on a Win app this is a piece of cake, you can use
Control.Invoke to have a delegate processed on the thread owning the
control, as your "main thread" should be the UI thread, where you declare
the controls then you get what you need.

Cheers,
 
Does the event code _have_ to run in Thread A, or do you just not want for
it to block thread B? If the former, you are going to have to come up with
some way to interrupt Thread A.

If the latter, you can just use another thread, or you can implicitly use
another thread by using the BeginInvoke capability of the delegate (causes
an event to be fired asynchronously).
Example (something like this, I haven't tried compiling):
Instead of this:
(Thread B firing the event):
if(this.MyEvent != null)
this.MyEvent(theParams)

You could do something like this (syntax might be off)
// this causes the event handler code to execute on a 3rd thread
if(this.MyEvent != null)
this.MyEvent.BeginInvoke(theParams);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top