1、在项目开发的中,经常会遇到要定时执行的任务,今天我们写一个在Global.asax.cs程序中的定时任务写法。
因为项目执行是从Application_Start() 方法开始的,所以我们写定时任务,需要将定时计划在这个方法中定义。
将下面代码放在Application_Start() 方法的最后面。
1 #region 预警消息推送定时-5分钟一次 2 3 int TimerS = 5 * 60 * 1000; 4 5 System.Timers.Timer tm = new System.Timers.Timer(TimerS); 6 tm.Elapsed += tm_Elapsed; 7 tm.AutoReset = true; 8 tm.Enabled = true; 9 #endregion
在Global.asax.cs新建一个定时任务的方法,如下所示
//定时执行推送 private void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { SearchModuleBLL service = new SearchModuleBLL(); service.PushSKMessages(); }