Annonymous event handler based on Timer : Timer : Development C# Examples


C# Examples » Development » Timer »

 

Annonymous event handler based on Timer









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

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

            mc.Elapsed  +=  delegate(object  obj,  EventArgs  e){
                                              Console.WriteLine("This  is  the  anonymous  method.");
                                        };

            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

This is the anonymous method.


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Development
» Timer