9

Is there a problem if I subscribe to the same event three times with the eventHandler? e.g.

a.SomethingChanged += new EventHandler(ChangeHandler);
a.SomethingChanged += new EventHandler(ChangeHandler);
a.SomethingChanged += new EventHandler(ChangeHandler);

Does this cause ChangeHandler to be invoked 3 times instead of 1? What is best way to handle this?

Note that these redundancies are not together but different areas of code paths.

Similary, is there an issue with unsubscribing from an event that is not registered? e.g.

a.SomethingChanged -= new EventHandler(ChangeHandler);  //ChangeHandler was never registered
ChrisF
  • 134,786
  • 31
  • 255
  • 325
user236215
  • 7,278
  • 23
  • 59
  • 87
  • See this question: http://stackoverflow.com/questions/817592/avoid-duplicate-event-subscriptions-in-c-sharp, basically, yes but you can avoid it. – Sean Airey Jan 22 '13 at 20:02
  • 1
    try it out and see what happens! I'm curious though, what's the idea behind having exclusive paths of execution when the same result is included in each of them? – B L Jan 22 '13 at 20:03
  • so basically there is no side effect of unsubscribing (-=) if it doesn't already registered. Correct? – user236215 Jan 22 '13 at 20:08
  • Why would you subscribe to the same event multiple times? What problem exactly are you trying to solve? – Security Hound Jan 22 '13 at 20:09
  • There are multiple code paths that might require it. So I want to avoid checking if it is already registered again and again. Thx – user236215 Jan 22 '13 at 20:17

2 Answers2

14

If you subscribe to an an event more than once then your handler will be called the corresponding number of times - in your example three.

Whether this is a problem or not depends on what your event handler does, but I'm assuming that you don't want it to be called multiple times.

There is no problem in unsubscribing from an event that you haven't subscribed to.

So if you're not sure what state your application is in (though you really should be) you can have:

a.SomethingChanged -= ChangeHandler;
...
a.SomethingChanged += ChangeHandler;

(Note: the new EventHandler(...) is syntactic sugar and can be omitted)

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • Well, it may or may not be a problem to subscribe to an event more than once. You may *want* to have the event handler triggered multiple times. – Servy Jan 22 '13 at 20:05
  • @Servy - strictly speaking you're right, but that's not the intention of the question. – ChrisF Jan 22 '13 at 20:06
5

Is there a problem if I subscribe to the same event three times with the eventHandler?

Nope, it will just add the event handler three times.

Does this cause ChangeHandler to be invoked 3 times instead of 1?

Yes.

What is best way to handle this?

That depends on what you want; which you haven't specified. If you want a way to add an event handler if and only if it hasn't already been added, then just remove the event handler and then add it again:

a.SomethingChanged -= new EventHandler(ChangeHandler);
a.SomethingChanged += new EventHandler(ChangeHandler);

Is there an issue with unsubscribing from an event that is not registered?

No, it will just do nothing.

Servy
  • 202,030
  • 26
  • 332
  • 449