Remove event handler : Timer : Development C# Examples


C# Examples » Development » Timer »

 

Remove event handler









    
using  System;
using  System.Timers;                      
using  System.Threading;                

class  MainClass
{
      public  static  void  TimerHandlerA(object  obj,  EventArgs  e)  {  
            Console.WriteLine("Class  A  handler  called");  
      }

      public  static  void  TimerHandlerB(object  obj,  EventArgs  e)  {  
            Console.WriteLine("Class  B  handler  called");  
      }

      static  void  Main()
      {
          
            MyTimerClass  mc  =  new  MyTimerClass();  

            mc.Elapsed  +=  TimerHandlerA;              
            mc.Elapsed  +=  TimerHandlerB;      

            mc.Elapsed  -=  TimerHandlerB;      
            
            Thread.Sleep(2000);                                      
      }
}

public  class  MyTimerClass
{
      public  event  EventHandler  Elapsed;

      private  void  OnOneSecond(object  obj,  EventArgs  e)
      {
            if  (Elapsed  !=  null)
                  Elapsed(obj,  e);
      }
      
      private  System.Timers.Timer  MyPrivateTimer  =  new  System.Timers.Timer();
      
      public  MyTimerClass()
      {
            MyPrivateTimer.Elapsed  +=  OnOneSecond;

            MyPrivateTimer.Interval  =  1000;

            MyPrivateTimer.Enabled  =  true;
      }
}
    
   
  
   



Output

Class A handler called


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Development
» Timer