How to determine if a event is handled?

I want to prevent a single event being hooked more than 1 time.  How can I determine if a event is hooked to a handler and the number of time it is hooked

if("myButton.Click not being handled")

  myButton.Click += new EventHandler(myButton_Click);

Thanks




Answer this question

How to determine if a event is handled?

  • ParamSE

    You can't, only when the event  is a direct member you can use GetInvocationList to iterate over all subscribers:


    foreach (Delegate subcriber in MyEvent.GetInvocationList())
    {
     Console.WriteLine(subcriber.Method.Name);
    }

     

    Otherwise you can allways use a flag:

     You can't, only when the event  is a direct member you can use GetInvocationList to iterate over all subscribers:


    private bool _wired = false;
    private void WireClickEvent()
    {
     if( !_wired )
     {
      _myButton.Click += new EventHandler( ... );
      _wired = true;
     }
    }

     



  • How to determine if a event is handled?