System.Timers.Timer定时执行程序示例代码

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了System.Timers.Timer定时执行程序示例代码脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
System.Timers.Timer 定时执行程序
复制代码 代码如下:

System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒
PRivate void Form1_Load(object sender, Eventargs e)
{
t.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp);
t.AutoReset = false; //每到指定时间Elapsed事件是触发一次(false),还是一直触发(true)
}
private void BTnStart_Click(object sender, EventArgs e)
{
t.Enabled = true; //是否触发Elapsed事件
t.Start();
}
private void Timer_TimesUp(object sender, System.Timers.ElapsedEventArgs e)
{
//到达指定时间5秒触发该事件输出 Hello World!!!!
System.Diagnostics.Debug.WrITeLine("Hello World!!!!");
}
private void btnStop_Click(object sender, EventArgs e)
{
t.Stop();
System.Diagnostics.Debug.WriteLine("未到指定时间5秒提前终结!!!");
}

web的定时清理缓存可以将
复制代码 代码如下:

System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒
t.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp);
t.AutoReset = false; //每到指定时间Elapsed事件是触发一次(false),还是一直触发(true)
t.Enabled = true; //是否触发Elapsed事件
t.Start();

五行code放到gloab.cs的Application_Start中去,启动web时,就启动;
如果是某个逻辑功能的定时,可以将code放到逻辑功能的类的静态构造函数中,在该逻辑类第一次执行时,静态构造函数会被调用,则定时自然启动。

脚本宝典总结

以上是脚本宝典为你收集整理的System.Timers.Timer定时执行程序示例代码全部内容,希望文章能够帮你解决System.Timers.Timer定时执行程序示例代码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: