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放到逻辑功能的类的静态构造函数中,在该逻辑类第一次执行时,静态构造函数会被调用,则定时自然启动。

您可能感兴趣的文章:

  • 解析php中获取系统信息的方法
  • 远程或本地获取系统信息的脚本RGIS.vbs
  • C#利用System.Threading.Thread.Sleep即时输出信息的详解
  • Linux下semop等待信号时出现Interrupted System Call错误(EINTR)解决方法
  • java system类使用方法示例 获取系统信息
声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。