Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When exactly are events executed in C#?

I have developed a C# application which makes heavy use of events. Now this application is occasionally doing funny things that I cannot understand or track down to a specific cause why they should occur. I reckon that the cause for these intermittent malfunctions is some sort of concurrency or race condition which I did not anticipate.

How exactly are events handled in C#? If an event is raised, will (a) the portion of code attached to that event be executed immediately? Or will the event (b) be put on a stack of events and be executed whenever .NET deems it suitable for execution while other code is executed in the meantime?

like image 795
Geoff Avatar asked May 18 '11 12:05

Geoff


People also ask

What is executed when an event occurs?

What Happens When an Event Occurs? What Happens When an Event Occurs? When an event occurs, the Event Manager automatically invokes all event handlers that subscribe to the event. The event handlers receive an input object containing run-time information.

What is event handling in C?

C event handlers allow you to interface more easily to external systems, but allowing you to provide the glue logic. The POS includes a small open source C compiler (TCC) which will dynamically compile and link your C code at runtime. Alternatively, you can precompile and ship a DLL/EXE with your functions.

When should I use events C#?

Events are typically used to signal user actions such as button clicks or menu selections in graphical user interfaces. When an event has multiple subscribers, the event handlers are invoked synchronously when an event is raised. To invoke events asynchronously, see Calling Synchronous Methods Asynchronously.

When would you use an event handler?

Use the EventHandler delegate for all events that don't include event data. Use the EventHandler<TEventArgs> delegate for events that include data about the event. These delegates have no return type value and take two parameters (an object for the source of the event and an object for event data).


4 Answers

If an event is raised, will the portion of code attached to that event be executed immediately?

Well, yes and no. Events are multicast delegates, so there might be zero, one or many "portions of code" attached to an event. In the scenario where there are many, clearly one of them has to go first and one of them has to go second. The one that goes second isn't executed immediately upon the event being raised; it's executed immediately upon the first event handler completing normally.

will the event be put on a stack of events and be executed whenever .NET deems it suitable for execution while other code is executed in the meantime?

Suppose your application is badly written and hangs the UI. While the UI is hung, the user clicks on button 1 and button 2. Since the application is hung, nothing visible happens. The events for button 1 and button 2 being clicked do not fire. But Windows has created a message queue and enqueued on it the fact that button 1 and button 2 have pending clicks that need to be processed when the application unhangs itself. When the message loop is pumped then the button 1 click event fires. When it is done doing its thing, the message loop is pumped again and the button 2 click event fires.

So yes, in that sense events are queued up and executed later, but it is not "when .NET deems it suitable"; it's when the thread that is processing the message queue starts processing the message queue again. There's no mysterious Windows policy in here controlling your code.

like image 140
Eric Lippert Avatar answered Oct 12 '22 00:10

Eric Lippert


It entirely depends on the event raising (and subscription) code.

If you're raising the event like this:

EventHandler handler = MyEvent;

if (handler != null)
{
    handler(this, EventArgs.Empty);
}

or something similar, then all the event handlers will be executed immediately. That's the typical implementation... you have to work a bit harder to put each event delegate into WinForms message queue or something like that.

If you could give us more information about what events you're talking about and how they're implemented, we may be able to help you more.

For more information on events and delegates (and the difference between them) you may wish to read my article on the topic.

like image 27
Jon Skeet Avatar answered Oct 12 '22 00:10

Jon Skeet


C# events, just like the rest of delegates, are executed immediately when triggered.

like image 22
user703016 Avatar answered Oct 11 '22 23:10

user703016


Unless explicitly implemented otherwise, events are called synchronously.

Typically the code that triggers an event looks like that:

public event EventHandler MyEvent;

protected virtual void OnMyEvent()
{
    EventHandler handler = MyEvent; // keep a copy to avoid race conditions
    if (handler != null)
        handler(this, EventArgs.Empty);
}

As you can see from this code, event handlers are called immediately and synchronously from the OnMyEvent method.

like image 34
Thomas Levesque Avatar answered Oct 12 '22 01:10

Thomas Levesque