For example, I have a class that fires an event, and 1000 subscribers to that event. Is a single thread used to fire each subscriber delegate one-by-one? Or does .Net use a thread pool to process some or all of the subscriptions in parallel?
Asked
Active
Viewed 3,042 times
11
-
1Serially, although you can change the event-raising code to parallelize it yourself. (Did you try it before asking the question?) – Jon Skeet Jun 17 '14 at 06:29
-
Both ways, refer [this](http://stackoverflow.com/questions/1516119/c-sharp-events-how-to-process-event-in-a-parallel-manner) – Nilay Vishwakarma Jun 17 '14 at 06:31
-
Why don't you try it yourself? Subcribe 1000 times and save the time the method gets executed. Good question through. – Kimmax Jun 17 '14 at 06:32
2 Answers
9
As Tigran said, event invocation is serial. Even more if one of the subscribers throws an exception at some point the rest of them will not be triggered.
The easiest way to trigger an event in parallel will be
public event Action Event;
public void Trigger()
{
if (Event != null)
{
var delegates = Event.GetInvocationList();
Parallel.ForEach(delegates, d => d.DynamicInvoke());
}
}
This implementation will suffer from same problem in case of an exception.

Alex des Pelagos
- 1,170
- 7
- 8
6
As is, event are simple serial invocation. If you want you can run it in async way, but this is an implementation detail.
In short: there is no any built-in parallelization or async of standard .NET
events, it's up to you to implement it.

Tigran
- 61,654
- 8
- 86
- 123