Asynchronous event firing

  • Thread starter Thread starter gs_sarge
  • Start date Start date
G

gs_sarge

Hi:

I was curious whether events fired as a multi-cast delegate were fired
asynchronously or not. If the invoked delegate is OneWay, do I need
to call GetInvocationList() (I think that's the name) and fire each
one asynchronously to take advantage of the OneWay delegate, or will
firing the event with one call do that for me?

Thanks in advance for any help in this question.

Steve
 
Hi:

I was curious whether events fired as a multi-cast delegate were fired
asynchronously or not.

How are you raising the event? If you use the standard language mechanisms
then they are synchronous:

public event MyDelegate OnTest;

void Test()
{
// do something
OnTest();
}

This means that however many delegates are added to the event OnTest they
will all be invoked one after another on the same thread. You can access the
invocation list and invoke the delegates on another thread if you wish.
If the invoked delegate is OneWay,

[OneWay] means that *no* return values of any type will be returned from the
delegate, not even exceptions. Although it is useful for async calls, its
not required.
do I need
to call GetInvocationList() (I think that's the name) and fire each
one asynchronously to take advantage of the OneWay delegate, or will
firing the event with one call do that for me?

No. If you have multiple delegates in an event (or in a multicast delegate
for that matter), then any out parameters and return values are irrelevent.
The only return values will be returned from the *last* delegate invoked.
Hence in general delegates that are called through a multicast delegate
should not have any return values unless you make a special effort to call
each delegate separately (through the invocation list). Thus [OneWay]
actually makes *more* sense on a multicast delegate when you use the default
invocation mechanism. Note that the attribute prevents exception
propagation, which is useful if you write a component and a third party
class calls your code: you don't know if their event handlers are written
correctly and by using [OneWay] you ensure that all event handlers are
called even if one throws an exception.

Richard
 
thanks for the information.

Hi:

I was curious whether events fired as a multi-cast delegate were fired
asynchronously or not.

How are you raising the event? If you use the standard language mechanisms
then they are synchronous:

public event MyDelegate OnTest;

void Test()
{
// do something
OnTest();
}

This means that however many delegates are added to the event OnTest they
will all be invoked one after another on the same thread. You can access the
invocation list and invoke the delegates on another thread if you wish.
If the invoked delegate is OneWay,

[OneWay] means that *no* return values of any type will be returned from the
delegate, not even exceptions. Although it is useful for async calls, its
not required.
do I need
to call GetInvocationList() (I think that's the name) and fire each
one asynchronously to take advantage of the OneWay delegate, or will
firing the event with one call do that for me?

No. If you have multiple delegates in an event (or in a multicast delegate
for that matter), then any out parameters and return values are irrelevent.
The only return values will be returned from the *last* delegate invoked.
Hence in general delegates that are called through a multicast delegate
should not have any return values unless you make a special effort to call
each delegate separately (through the invocation list). Thus [OneWay]
actually makes *more* sense on a multicast delegate when you use the default
invocation mechanism. Note that the attribute prevents exception
propagation, which is useful if you write a component and a third party
class calls your code: you don't know if their event handlers are written
correctly and by using [OneWay] you ensure that all event handlers are
called even if one throws an exception.

Richard
 
Hi
1. If i define an event handler as OneWay, the Callback method gets called before the event handler, instead of after. Why is this happening?
2. Will the callback be executed in the caller's thread or in the thread which handles the event
3. Is it necessary to set the AsyncCallback method as OneWay. What is the use of setting this method as OneWay

Thanks for any help.
 
Back
Top