在winform中我们常常需要用到定时器,system.timers.timer定时器的一般用法如下:
using System;
using System.Timers;
public class Timer1
{
private static System.Timers.Timer aTimer;
public static void Main()
{
//新建定时器对象,设置定时为10秒
aTimer = new System.Timers.Timer(10000);
//添加定时溢出事件的回调方法
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
//启动定时器
aTimer.Enabled = true;
//假如定时器已经倒计时到还剩5秒就要触发溢出事件了,重新设置Interval的值
//定时器会重新计时
aTimer.Interval = 20000;
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);
}
}