在Global.asax.cs中开启定时器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public void Application_Start(object sender, EventArgs e) { logger.Warn("Application_Start"); JobManager.Init();//定时任务 }
在JobManager.cs中设置定时器
public class JobManager { Timer Create_Order_Timer; /// <summary> /// 初始化定时任务,只可在应用开始时调用一次 /// </summary> public static void Init() { new JobManager().StartAll(); } /// <summary> /// 开始所有任务 /// </summary> void StartAll() { //自动下载订单 Create_Order_Timer = new Timer(40000); Create_Order_Timer.Elapsed += new ElapsedEventHandler(OnTimedEvent_Create_Order); Create_Order_Timer.Enabled = true; } //定时生成订单 void OnTimedEvent_Create_Order(object sender, ElapsedEventArgs e) { try { //每分钟检查一次任务 Auto_Create_Order.Auto_Create_Order_Task(); Create_Order_Timer.Interval = 1000 * 60; Create_Order_Timer.Enabled = true; } catch (Exception ex) { logger.Error("", ex); //throw; } } }