views:

630

answers:

4

If I set up multiple event handlers, like so:

_webservice.RetrieveDataCompleted += ProcessData1;
_webservice.RetrieveDataCompleted += ProcessData2;

what order are the handlers run when the event RetrieveDataCompleted is fired? Are they run in the same thread and sequentially in the order that are registered?

+2  A: 

They are run in the order in which they are registered. RetrieveDataCompleted is a Multicast Delegates. I am looking through reflector to try and verify, and it looks like an array is used behind the scenes to keep track of everything.

Bob
Thanks for the reference.
pst
+5  A: 

The order is arbitrary. You cannot rely on the handlers being executed in any particular order from one invocation to the next.

Edit: And also - unless this is just out of curiosity - the fact that you need to know is indicative of a serious design problem.

Rex M
+1 for the editorial comment.
Matt Davis
+5  A: 

Currently, they are executed in the order they are registered. However, this is an implementation detail, and I would not rely on this behavior staying the same in future versions, since it is not required by specifications.

Reed Copsey
This is important. It's entirely possible for a future version of something to, e.g., execute different handlers on different threads. Do not create a dependency on this implementation detail!
Greg D
Executing on different threads is unlikely, since that would break compatibility drastically. However, a change of data structure for internal storage could change this behavior just as easily.
Reed Copsey
I wonder, why the downvotes? This is exactly true, and answers the question directly...
Reed Copsey
Further question: 7.2.4 in the C# language spec says "...the result of the (addition) operation is a new delegate instance that, when invoked, invokes the first operand and then invokes the second operand". Do I have the wrong end of the stick, or does this say that the first should always get called before the second?Ref: http://msdn.microsoft.com/en-us/library/aa691375%28VS.71%29.aspx
Rawling
@Rawling: That's for binary operator overload resolution - not event handling. This isn't the addition operator, in this case.
Reed Copsey
OK... still from the spec, how about here: http://msdn.microsoft.com/en-us/library/aa664605%28VS.71%29.aspx"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."The code excerpt also shows the delegates called in the order they're added.Event handlers are delegates, right?
Rawling
Ah, I see where I'm going wrong: "Event handlers are delegates, right?". I now know they're not.Have written myself an event that fires handlers in reverse order, just to prove it to myself :)
Rawling
+2  A: 

The invocation list of a delegate is an ordered set of delegates in which each element of the list invokes exactly one of the methods invoked by the delegate. An invocation list can contain duplicate methods. During an invocation, a delegate invokes methods in the order in which they appear in the invocation list.

From here: Delegate Class

Philip Wallace
Thanks for the good answer, excerpt and link to the reference.
pst