Events Despatching Multiple Methods

G

Gene Wirchenko

Dear C-Sharpies:

I am studying Wrox's "Professional C# 3rd Edition" by Robinson et
al. Chapter 6 (Delegates and Events) is a tough slog.

On page 179, it ends the Multicast Delegates section with "If you
are using multicast delegates, you should be aware that the order in
which methods chained the same delgate will be called is formally
undefined. You should, therefore, avoid writing code that relies on
such methods being called in any particular order."

The next section is Events. One of the buttons in the sample
code has two methods called
btnTwo.Click+=new EventHandler(Button_Click);
btnTwo.Click+=new EventHandler(btnTwo_Click);
Button_Click() indicates which button was clicked. btnTwo_Click()
pops up a message box.

IIUC, the p. 179 caveat quoted above applies.

It could well matter which order the methods are called in, and
C# is supposedly a professional language so it ought to be possible to
specify the order. If the order does matter, how does one code for
it?

Or am I missing something?

Sincerely,

Gene Wirchenko
 
A

Arne Vajhøj

I am studying Wrox's "Professional C# 3rd Edition" by Robinson et
al. Chapter 6 (Delegates and Events) is a tough slog.

On page 179, it ends the Multicast Delegates section with "If you
are using multicast delegates, you should be aware that the order in
which methods chained the same delgate will be called is formally
undefined. You should, therefore, avoid writing code that relies on
such methods being called in any particular order."

The next section is Events. One of the buttons in the sample
code has two methods called
btnTwo.Click+=new EventHandler(Button_Click);
btnTwo.Click+=new EventHandler(btnTwo_Click);
Button_Click() indicates which button was clicked. btnTwo_Click()
pops up a message box.

IIUC, the p. 179 caveat quoted above applies.

It could well matter which order the methods are called in, and
C# is supposedly a professional language so it ought to be possible to
specify the order. If the order does matter, how does one code for
it?

Or am I missing something?

I am not convinced that it is correct.

http://msdn.microsoft.com/en-us/library/system.multicastdelegate.aspx

<quote>
A MulticastDelegate has a linked list of delegates, called an invocation
list, consisting of one or more elements. When a multicast delegate is
invoked, the delegates in the invocation list are called synchronously
in the order in which they appear. If an error occurs during execution
of the list then an exception is thrown.
</quote>

C# spec:

<quote>
Invocation of a delegate instance whose invocation list contains
multiple entries proceeds by invoking each of the methods in the
invocation list, synchronously, in order. Each method so called is
passed the same set of arguments as was given to the delegate instance.
If such a delegate invocation includes reference parameters (§1.92.1.2),
each method invocation will occur with a reference to the same variable;
changes to that variable by one method in the invocation list will be
visible to methods further down the invocation list. If the delegate
invocation includes output parameters or a return value, their final
value will come from the invocation of the last delegate in the list.
If an exception occurs during processing of the invocation of such a
delegate, and that exception is not caught within the method that was
invoked, the search for an exception catch clause continues in the
method that called the delegate, and any methods further down the
invocation list are not invoked.
</quote>

But if it is correct and you need a workaround then just use
a single delegate and a custom queue of delegates to invoke
from that.

Arne
 

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