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)?
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With