Events add and remove with synchronized block : Event : GUI Windows Forms C# Examples


C# Examples » GUI Windows Forms » Event »

 

Events add and remove with synchronized block









    
using  System;
using  System.Collections;
using  System.Runtime.CompilerServices;
public  class  Button
{
        public  delegate  void  ClickHandler(object  sender,  EventArgs  e);
        
        Hashtable  delegateStore  =  new  Hashtable();
        static  object  clickEventKey  =  new  object();
        
        public  event  ClickHandler  Click
        {
                [MethodImpl(MethodImplOptions.Synchronized)]
                add
                {
                        delegateStore[clickEventKey]  =
                        Delegate.Combine((Delegate)  delegateStore[clickEventKey],
                        value);
                }
                
                [MethodImpl(MethodImplOptions.Synchronized)]
                remove
                {
                        delegateStore[clickEventKey]  =
                        Delegate.Remove((Delegate)  delegateStore[clickEventKey],
                        value);
                }
        }
        
        protected  void  OnClick()
        {
                ClickHandler  ch  =  (ClickHandler)  delegateStore[clickEventKey];
                if  (ch  !=  null)
                ch(this,  null);
        }
        
        public  void  DoClick()
        {
                OnClick();
        }
}

class  MainClass
{
        static  public  void  ButtonHandler(object  sender,  EventArgs  e)
        {
                Console.WriteLine("Button  clicked");
        }
        
        public  static  void  Main()
        {
                Button  button  =  new  Button();
                
                button.Click  +=  new  Button.ClickHandler(ButtonHandler);
                
                button.DoClick();
                
                button.Click  -=  new  Button.ClickHandler(ButtonHandler);
                
                button.DoClick();
        }
}
    
   
  
   



Output

Button clicked


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo GUI Windows Forms
» Event