Timer组件是基于服务器的计时器,通过设置时间间隔Interval,周期性的触发Elapsed事件。
用法如下:
代码如下:
class Program {
static System.Timers.Timer Timer1 = new System.Timers.Timer();
static void Main() {
Timer1.Interval = 1000;
Timer1.Elapsed += new ElapsedEventHandler(PeriodicTaskHandler);
Timer1.Start();
Console.ReadLine();
}
static void PeriodicTaskHandler(object sender, ElapsedEventArgs e) {
string str =Thread.CurrentThread.ManagedThreadId.ToString()+”##” +”Timer1″ +”##” + e.SignalTime.ToLongTimeString();
Console.WriteLine(str);
}
}