Are event callback delegates executed executed in a separate thread????

B

Bob Rock

Hello,

I was wondering if callback delegates, called in response to a event,
are executed in their own thread. I was suspecting the OS might spawn
a new thread and have the delegate execute in that thread but having
written a small sample application this does not seem to be happening.

I'm writing an application that will be receiving messages from a
Tibco RV messaging bus and from MSMQ and that will also have to handle
those messages (elaborate them and take appropriate actions). I was
worried that if no threads are created to execute my C# delegates in
response to an event (a new Tibco message or a new MSMQ message), in
periods of heavy messaging and message handling/elaboration, messages
might start to just gather and in the end might start to get dropped.

If callback methods (delegates) were indeed executed in a separate
thread this situation would probably never occur, not being so (or at
least it seems) I'm facing the possibility of having to write a
multithreaded application ... which I would very much like to avoid.
Any suggestion???


Bob Rock
 
K

Ken Kolda

When you invoke an event the delegates are called on the same thread as
raised the event (which means they're called sequentially).

Ken
 
K

Kevin Aubuchon

If the callback method is not invoked asynchronously, you can still invoke
your delegate asynchronously. Have the callback method do something like
myDelgate.BeginInvoke(....). The myDelegate() will execute on a thread from
the pool, and the event handler will return immediately. There's no reason
one delegate can't invoke another.

good luck
kevin aubuchon
 
B

Bob Rock

If the callback method is not invoked asynchronously, you can still invoke
your delegate asynchronously. Have the callback method do something like
myDelgate.BeginInvoke(....). The myDelegate() will execute on a thread from
the pool, and the event handler will return immediately. There's no reason
one delegate can't invoke another.

good luck
kevin aubuchon

Thank you Kevin,

the problem is that if the application is heavely busy it may take
sometime for your callback function to be called and thus your
delegate to be fired.

Anyone have any other idea???
Is there anything that makes the OS implicitly spawn a new thread to
do that something???


Bob Rock
 
D

David Levine

the problem is that if the application is heavely busy it may take
sometime for your callback function to be called and thus your
delegate to be fired.

Anyone have any other idea???
Is there anything that makes the OS implicitly spawn a new thread to
do that something???

You can start your own thread, raise its priority level, and deliver the
events on that thread. The thread can block on an event waiting for a msg to
arrive. The input would add the msg to a list and set the event (waking up
the thread). The thread pulls msgs off the list and invokes the delegate
synchronously (ctl.Invoke) until there are no msgs left on the list.

You'll have to provide locks for thread safety (add/remove from the list).
Other then that it ought to be straightforward.
 

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

Top