public class MyClass
{
private static Timer timer; //声明一个全局的timer.
static MyClass()
{
timer = new Timer(MyMethod, null, new TimeSpan(0, 0, 30), new TimeSpan(0, 5, 0)); //30秒后启动,每隔5分钟运行一次
}
public static MyMethod()
{
//定时执行的代码
}
}
然后在Global文件中的Application_Start方法中,访问这个类就可以了
注:这里的Timer是System.Threading.Timer;

本文介绍如何使用 C# 的 System.Threading.Timer 实现定时任务,通过实例展示了一个每5分钟执行一次的任务设置方法,并说明了如何在应用程序启动时初始化此类。
2938

被折叠的 条评论
为什么被折叠?



