I have a code that attaches an event to a form.
this.form.Resize += new EventHandler(form_Resize);
As you see this is done by +=
.
If the above code is executed twice or multiple times,
like this:
this.form.Resize += new EventHandler(form_Resize);
this.form.Resize += new EventHandler(form_Resize);
this.form.Resize += new EventHandler(form_Resize);
this.form.Resize += new EventHandler(form_Resize);
this.form.Resize += new EventHandler(form_Resize);
Is the callback method attached multiple times?
How many times will be called the method
form_Resize
?
Does an event gets executed multiple times if it's callback method was assigned multiple times to the same object?
The event handler will get called once for each time it is attached. (C#)
To guard against double attachment you can use this pattern:
this.form.Resize -= new EventHandler(form_Resize);
this.form.Resize += new EventHandler(form_Resize);
The first statement will not throw an error if there are no handlers attached and will remove an existing handler.
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