定时器,第1次就是在间隔的秒数之后才触发

3/8/2017来源:ASP.NET技巧人气:2228

using System;
using System.Data.SqlClient;

namespace Consoleapplication1
{
    class PRogram
    {
        private static System.Timers.Timer timer = new System.Timers.Timer();

        static void Main(string[] args)
        {
            Console.WriteLine("Main       -> {0:HH:mm:ss}", DateTime.Now);
            timer.Enabled = true;
            timer.Interval = 5 * 1000;
            timer.Start();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerEvent);
            Console.Read();
        }

        private static void TimerEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            Console.WriteLine("TimerEvent -> {0:HH:mm:ss}", DateTime.Now);
        }
    }
}

如果想最开始就触发, 只能自己先调用一次了。