C#基础(007)---TimerElapsed 事件

//TimerElapsed 事件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace ConsoleApplication1
{
    class Program
    {      

        //TimerElapsed 事件---达到间隔时发生。

        private static System.Timers.Timer aTimer;
        public static void Main()
        {
          
            aTimer = new System.Timers.Timer();
            // Hook up the Elapsed event for the timer.   
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
              
            aTimer.Interval = 2000;
            aTimer.Enabled = true;
            Console.WriteLine("Press the Enter key to exit the program.");
            Console.ReadLine();
      
        }
     
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
        }
    }
}


 
/* This code example produces output similar to the following: 
 * Press the Enter key to exit the program.
 * The Elapsed event was raised at 5/20/2007 8:42:27 PM
 * The Elapsed event was raised at 5/20/2007 8:42:29 PM
 * The Elapsed event was raised at 5/20/2007 8:42:31 PM ... 
 */

posted on 2014-04-20 02:43  lbsf  阅读(2849)  评论(0)    收藏  举报

导航