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.)
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 :)
Related
I'm building a project using the Backbone global as an event bus. The idea is that a view will trigger a custom event and pass in some data, then whatever collection is listening for that event will use the data to create a model and add that model to itself, and give some kind of message.
I notice that if I start from a new instance of my application and trigger one of these events, then in Chrome Dev Tools, Backbone._events looks something like this: {'add_person': [Object] } and the appropriate event handler runs and processes that one object in the value array. So far, so good.
Now, let's say I trigger that event handler again. What I've noticed is that Backbone._events looks like {'add_person': [Object, Object]} and the event handler runs twice, presumably on both members of the value array, including the first which was already processed.
My question is how do I prevent this behavior, short of binding the event with once and re-binding it after each time it's called? Thanks.
I'm not entirely sure what your setup is, i.e. how you're triggering and listening to these events you're firing but it sounds like you might be doing something like:
GlobalEventBus.on('add_person', this.addPerson, this);
Assuming you're binding to the event using on this could well be your problem if you're not manually unbinding the listener when your view is destroyed.
You're better off using Backbone's listenTo when listening to events that are triggered from an object outside the object you're listening in.
So, for example, in your collection you'd instead have:
this.listenTo(GlobalEventBus, 'add_person', this.addPerson)
Again though, i'm not entirely sure what your setup is.
See this codepen for a working example.
Only one events is triggered each time but keep in mind that only one model is being added because each model is a duplicate and has the same id. Assuming each model was unique it would be added to the collection.
Some further info on listenTo
I am facing an issue where I need to stop propagation of event from event aggregator(Prism Framework).
Is there any way I could do it?
Say I have 10 handlers for an event. Now, i am checking some condition in second event handler and if that condition evaluate to true, i want to stop further processing of the event by rest of 8 event handler.
That isn't really the way the observer pattern works. The EventAggregator is great for decoupling classes - various things that are interested in a message can subscribe to receive it, and anything that wants can publish that message out. All without having to know (a) who is sending and (b) who is receiving.
You have no control of the order the message is received, or cancelling it once it has been sent.
You may be able to make use of the Subscription Filtering functionality of the EventAggregator, but it isn't something I've had to do before.
Basically we have a huge project, and we have an event handler that sometimes is triggered and some others it isn't. I think this is because somewhere in the jungle of code, we're handling that event, so it doesn't bubble up to where we need it. Is there anyway to find out where is it being handled?
Thanks!
Did you try to use Snoop..... there is an event Tab that tells us where the event is bubbled to and where its handled...
What is the difference between OnLoad method and Load event? I am developing WinForm controls.
Should I register to Load event or override the OnLoad method? What are the advantages and the disadvantages of each one?
I'd go for overriding OnLoad, so you spare the CPU cycles to invoke the event handler.
The general pattern is to override a method, if you inherit from a control; otherwise, subscribe to the event.
But remember to call the base class' OnLoad method, because that's where the Load event invoked.
OnLoad method is the one that raises Load event. It's a standard pattern in framework classes, and a generally recommended one - for any event Foo, you have a virtual protected method OnFoo which raises that event; and no other method of the class raises the event directly, but always calls OnFoo.
If you need to handle the event on this, it's usually both easier and faster to override OnFoo.
OnLoad is the default event handler used in VB.NET to handle the Load event. I typically override this method when I need to attach code to the load event. There are also default functions for the other Page Life Cycle events: OnPreRender, OnInit, etc.
Just to add a bit of clarity to the existing answers, an event handler is something that 'waits' for the event to trigger. This requires some CPU overhead as you need to have a thread 'listening' for the event to occur. This would be something like MainWindow_Load() in a winforms application. However, since we already know 'when' the event will happen (it happens as soon as the form loads), there's no reason to actually use an event handler. It's not like a click event where we have no clue when the user might click in which case you really do need an event handler. So instead of holding up some CPU processing, we can create a method in the class which simply forces the Load event to occur which is called invoking the Load event.
This is why it's preferable to use the OnLoad method when possible, in situations where you always know when the event is going to happen.
I have a 3D application in WPF which basically is supposed to zoom in and out as the MouseWheel event is fired. I have attempted to subscribe everything possible, but can't find what it is which is handling it.
Is there a way to find out what is? Or is there a way to have the event not handled, or for an encompassing UIElement to get to this event before/after the one dealing with it?
If you want to get called even though someone else has already handled the event, try subscribing to the event using the UIElement.AddHandled(RoutedEvent, bool) function. Pass in true as the second argument to get called even if the event has been handled.
You can also try subscribing to the PreviewMouseWheel event to get called when the event is tunneling.