tags:

views:

123

answers:

6

What will happen when 10 threads will subscribe to the same event and the event fires? Which thread will pick it up?

+11  A: 

Thread's don't subscribe to events, objects do. When an event fires, all of the registered handlers execute on the same thread (the one that raised the event). There's no built-in facility for events to fire on multiple threads.

A handler can choose to forward the event information to a separate thread, if desired, but that's not part of the built-in mechanism of event dispatch.

LBushkin
If you want a more asynchronous view of events, you could check out [Rx](http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx). Using Rx, it's pretty easy to marshal events to a particular thread (assuming it has a `SynchronizationContext` to marshal to, of course).
Stephen Cleary
i think threads are also objects... :)
ultrajohn
@ultrajohn: booooo! :)
Merlyn Morgan-Graham
in Java, Threads are objects... :)
ultrajohn
Threads are not objects. A threads is something the CPU and the OS gives you, it's an execution path, an execution context. You have objects that gives you access to the details related to those threads, but saying that "threads are objects" is false.
Lasse V. Karlsen
:), ehehe i get what you mean but in java, you can create a Thread object... :) nvm,... :)
ultrajohn
@ultrajohn you can do that in c# as well, but that has nothing to do with anything regarding events.
nos
+1  A: 

I think what you mean here is that multiple objects on separate threads subscribe to an event.

All of the handlers will be called but on the same thread that invoked the event.

Sandeep Singh Rawat
A: 

The answer to your question I guess is it depends on the implementation of the event dispatcher... Usually you use a list to keep track of all the event handlers which subscribed to a particular event, so most likely in terms of this kind of implementation, the first handler that gets fired is the first event handler that got subscribed if you of course call all the relevant procedures synchronously, if not, then it depends... just a thought..

ultrajohn
A: 

If you want to know which object will pick up the event, every object that subscribes to an event will pick up that event, but each will run on the thread that the event occurred on.

If you want to know which object will pick up that event first see ultrajohns answer.

Jack
A: 

I think if I understand your question. You mean to ask your object exposes a event that user of your object can subscribe. If 10 different users of your object has subscribed to this event, and at some point you fire the event, what would be order (or simultaneously) the event handlers would be invoked?

Answer: Since the event handler execution happens on the same thread who fires it (in this case your object's processing thread) can only process one handler function at a time. The order is not guaranteed (meaning not necessarily first subscriber would executed first and last would be executed last). I hope this answers your question. The bottom line is all 10 handler would be called and none would be in parallel. They will be executed one after another. I have seen people accidentally subscript to save event twice and then seeing the action happening twice and having hard time figuring out why some things are happening multiple times.

Jai MAthur
A: 

If by "event" you mean a Win32 synchronization Event (which is how I read the question) then it depends on how the EventWaitHandle is created. If its manual reset, the event will signal all threads and all will execute. If its auto reset, a single thread will be signalled and executed. Any of your 10 threads waiting on the event could be chosen.

ScottTx