C#自动化执行程——定时器Timer

System.Windows.Forms.Timer是应用于WinForm中的,

缺点是计时不精确,而且必须有消息循环,Console Application(控制台应用程序)无法使用。

使用System.Timers.Timer类

System.Timers.Timer t = new System.Timers.Timer(10000);//实例化Timer类,设置间隔时间为10000毫秒;
t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件;
t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;

public void theout(object source, System.Timers.ElapsedEventArgs e)
{
MessageBox.Show("OK!");
}

猜你喜欢

转载自www.cnblogs.com/StarZhai/p/10028971.html