Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling an event handler in C#

Tags:

c#

events

I've been trying to learn how to use event handlers in C# but I can't figure out what handler(this, e) does in the following code:

public event EventHandler ThresholdReached;

protected virtual void OnThresholdReached(EventArgs e)
{
    EventHandler handler = ThresholdReached;
    if (handler != null)
    {
        handler(this, e);
    }
}

Is it trying to call the event handler method (this) with the event (e)?

like image 253
Tony Avatar asked Aug 31 '12 14:08

Tony


People also ask

How do you call an event handler?

Call an event handler using Handles and WithEvents Make sure the event is declared with an Event Statement. Declare an object variable at module or class level, using the WithEvents keyword. The As clause for this variable must specify the class that raises the event.

How do you use event handlers?

To respond to an event, you define an event handler method in the event receiver. This method must match the signature of the delegate for the event you are handling. In the event handler, you perform the actions that are required when the event is raised, such as collecting user input after the user clicks a button.

What is an event handler example?

Here are some common examples of an event handler: A notification pops up on a webpage when a new tab is opened. A form is submitted when the submit button is clicked. The background color changes on a mouse click.

What is event handler in C sharp?

An event handler, in C#, is a method that contains the code that gets executed in response to a specific event that occurs in an application. Event handlers are used in graphical user interface (GUI) applications to handle events such as button clicks and menu selections, raised by controls in the user interface.


3 Answers

It invokes all registered event listeners which are registered on the ThresholdReached event.

The handler != null check makes sure at least one listener is registered to that event.

In C# 6.0 and above you can use Null Propagation:

handler?.Invoke(this, e);

handler(this, e) will call every registered event listener. Event listeners subscribe with help of the += operator and unsubscribe with -= operator to that event.

this is there to give the event listener to know who raised the ThresholdReached event. Who was the sender of the event.

e is the event argument which is also passed into the listener method which can contain more useful informations about the ThresholdReached event e.g. which threshold was reached.

like image 173
BlueM Avatar answered Nov 12 '22 08:11

BlueM


It is raising a ThresholdReached event with arguments sender=this and eventarguments = e. In fact, it is the same as the following;

public event EventHandler ThresholdReached;

protected virtual void OnThresholdReached(EventArgs e)
{
    if (ThresholdReached != null)
    {
        ThresholdReached(this, e);
    }
}

If there are any listeners to this event; it will simply call listener delegates;

this.ThresholdReached += new EventHandler(Form1_ThresholdReached);

Then, when this event is raised Form1_ThresholdReached function will be called with this and e parameters.

like image 28
daryal Avatar answered Nov 12 '22 07:11

daryal


The code in your example copies all registered handlers to the local variable handler, checks that the invocation list is not empty and invokes all members of the copied invocation list with the arguments this and e.

The reason for the fact that you get a snapshot of the current invocation list is that delegates are immutable. You get a reference to the current multicast delegate, and when handlers are added or removed the backing field points to a new delegate created from two immutable ones.

The usual reason to copy the invocation list to a local variable is some form of thread-safety: a handler could be unsubscribed between the usual nullity check (check that the invocation list isn't empty) and the actual invocation: that way you might accidentally fire an event with no handlers and a NullReferenceException would be thrown.

like image 20
DoomMuffins Avatar answered Nov 12 '22 06:11

DoomMuffins