Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if event is registered

Tags:

c#

events

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?

like image 747
Offler Avatar asked Jan 14 '13 14:01

Offler


People also ask

How do you check if an element has an event listener?

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.

What is event Registration in JavaScript?

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.

How do you override a click event?

Use the off() method to override jQuery event handlers. This method is used to remove an event handler.


2 Answers

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;
       }
   }

}
like image 200
Frederik Gheysels Avatar answered Oct 06 '22 01:10

Frederik Gheysels


You can use GetInvocationList() and get the count in turn

like image 27
Nasmi Sabeer Avatar answered Oct 06 '22 01:10

Nasmi Sabeer