System.Threading.Timer

class Program
{
   int TimesCalled = 0;
   void Display(object state)
   {
     Console.WriteLine("{0} {1} keep running.", (string)state, ++TimesCalled);
   }
   static void Main(string[] args)
   {
     Program p = new Program();
     //2秒后第一次调用,每1秒调用一次
     System.Threading.Timer myTimer = new System.Threading.Timer(p.Display, "Processing timer event", 2000, 1000);
     // 第一个参数是:回调方法,表示要定时执行的方法,第二个参数是:回调方法要使用的信息的对象,或者为空引用,第三个参数是:调用 callback 之前延迟的时间量(以毫秒为单位),指定 Timeout.Infinite 以防止计时器开始计时。指定零 (0) 以立即启动计时器。第四个参数是:定时的时间时隔,以毫秒为单位

     Console.WriteLine("Timer started.");
     Console.ReadLine();
   }
}

猜你喜欢

转载自www.cnblogs.com/jshchg/p/12950775.html
今日推荐