Brie
Brie

Reputation: 2359

Add/remove EventHandler multiple times in WPF

If I add (or remove) the same EventHandler to an Event multiple times, is that "bad"? Is anything happening internally which would cause problems here? (I am implementing data validation on a TextBox, which will turn on/off a MouseDoubleClick handler as the TB content becomes in/valid.)

Upvotes: 2

Views: 2046

Answers (1)

Kapitán Mlíko
Kapitán Mlíko

Reputation: 4538

Nothing really happens when you try to unsubscribe from event multiple times.

When you want to subscribe to event with your handler you can make sure it's not already subscribed. You should do that. Because it's possible to subscribe multiple times. of course only if it's not something you want to do...

What happens when you subscribe to the event twice with same handler? It's simple... handler is twice in event's InvocationList so it's called twice. When you subscribe again then it's called three times... every time the event is raised subscribed Handlers in InvocationList are called.

You can look here ...as you can see... It's duplicate of at least two already asked questions. So there are many answers :)

Upvotes: 2

Related Questions