Are C# events synchronous?

Alexander Bird picture Alexander Bird · Aug 18, 2011 · Viewed 27.8k times · Source

There are two parts to this question:

  1. Does raising an event block the thread, or does it start execution of EventHandlers asynchronously and the thread goes continues on at the same time?

  2. Are the individual EventHandlers (subscribed to the event) run synchronously one after another, or are they run asynchronously with no guarantee that others aren't running at the same time?

Answer

Daniel Hilgarth picture Daniel Hilgarth · Aug 18, 2011

This is a general answer and reflects the default behavior:

  1. Yes, it blocks the thread, if the methods subscribing to the event are not asynchronous.
  2. They are executed one after the other. This has another twist: If one event handler throws an exception, the event handlers not yet executed will not be executed.

Having said that, every class that provides events can choose to implement its event asynchronously. IDesign provides a class called EventsHelper that simplifies this.

[Note] this link requires you to provide an e-mail address to download EventsHelper class. (I am not affiliated in any way)