If I have the classic ways to register and unregister events (+= -=), is there also a way to see whether something is registered right now?
Let's say I have 2 methods which can register on one Timer. If something has already registered at .Elapsed
, I do not want anything else to register (and do not want something to register multiple times).
Is there any way to look up which methods are registered at the moment to a specific event?
Right-click on the search icon button and choose “inspect” to open the Chrome developer tools. Once the dev tools are open, switch to the “Event Listeners” tab and you will see all the event listeners bound to the element. You can expand any event listener by clicking the right-pointing arrowhead.
The simplest way to register an event handler is by setting a property of the event target to the desired event handler function. By convention, event handler properties have names that consist of the word “on” followed by the event name: onclick , onchange , onload , onmouseover , and so on.
Use the off() method to override jQuery event handlers. This method is used to remove an event handler.
If you really want such behaviour, I think the best option is to use the overload the add{} and remove{} functionality of the event.
public class Foo
{
private EventHandler<ElapsedEventArgs> _elapsed;
public EventHandler<ElapsedEventArgs> Elapsed
{
add
{
if( _elapsed == null )
_elapsed += value;
else
throw new InvalidOperationException ("There is already an eventhandler attached to the Elapsed event.");
}
remove
{
_elapsed -= value;
}
}
}
You can use GetInvocationList() and get the count in turn
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