c# 定时执行任务

在Global.asax文件中加上

复制代码
 1 void Application_Start(object sender, EventArgs e) 
 2     {
 3         // Code that runs on application startup
 4         
 5         Application["UserName"] = null;
 6         System.Timers.Timer aTimer = new System.Timers.Timer();
 7         aTimer.Elapsed +=new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
 8         // 设置引发时间的时间间隔 此处设置为1秒
 9         aTimer.Interval = 1000;
10         aTimer.Enabled = true;
11     }
12 
13  void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
14     {
15         // 得到 hour minute second  如果等于某个值就开始执行
16         int intHour = e.SignalTime.Hour;
17         int intMinute = e.SignalTime.Minute;
18         int intSecond = e.SignalTime.Second;
19         // 定制时间,在00:00:00 的时候执行
20         int iHour = 01;
21         int iMinute = 00;
22         int iSecond = 00;
23               // 设置 每天的00:00:00开始执行程序
24         if (intHour == iHour && intMinute == iMinute && intSecond == iSecond)
25         {
26             //调用你要更新的方法
27         }
28     }
复制代码

猜你喜欢

转载自blog.csdn.net/qmdweb/article/details/80969270