今天群里有人问起,怎样在asp.net网站上定时执行一定的任务
突然想起似乎可以在global.asax中启动一个timer,在另一个线程中定时调用特定函数来实现。
自己从未写过这方面的,于是找了下MSDN,最后成功运行了如下代码
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.Security;
6using System.Web.SessionState;
7using System.Threading;
8using System.IO;
9
10public class Global : System.Web.HttpApplication
11 {
12 Timer timer; //声明Timer变量,注意这是System.Threading.Timer
13
14 protected void Application_Start(object sender, EventArgs e)
15 {
16 logPath = Server.MapPath("~/timerlog.log");
17
18 Log("timer start at "+ DateTime.Now.ToString());
19
20 //启动定时器,this.TimerCallBack是回调函数,
21 //null是回调函数接收的参数
22 //0是延迟时间(单位是毫秒),这里用0表示立即执行
23 //1000是定时器周期(单位是毫秒),表示每1秒执行一次回调函数
24 timer =new Timer(this.TimerCallBack, null, 0, 1000);
25
26 }
27
28 //回调函数,这里实现了一个在文本文件中记录日志的功能
29 void TimerCallBack(object stateInfo)
30 {
31 Log(DateTime.Now.ToString());
32 }
33
34 string logPath;
35
36 void Log(string s)
37 {
38 StreamWriter sw =new StreamWriter(logPath, true);
39 sw.WriteLine(s);
40 sw.Close();
41
42 }
43
44 protected void Application_End(object sender, EventArgs e)
45 {
46
47 Log("timer stop at "+ DateTime.Now.ToString());
48 timer.Dispose();//销毁定时器,即停止定时器
49 }
50 }
51}
最后,得到了如下的日志文件
2 2010-03-30 23:03:46
3 2010-03-30 23:03:46
4 2010-03-30 23:03:46
5 2010-03-30 23:03:47
6 2010-03-30 23:03:47
7 2010-03-30 23:03:47
8 2010-03-30 23:03:47
9 2010-03-30 23:03:48
10 2010-03-30 23:03:48
11 2010-03-30 23:03:48
12 2010-03-30 23:03:48
13 2010-03-30 23:03:49
14 2010-03-30 23:03:49
15 2010-03-30 23:03:49
16 2010-03-30 23:03:49
17 2010-03-30 23:03:50
18 2010-03-30 23:03:50
19 2010-03-30 23:03:50
20 2010-03-30 23:03:50
21 2010-03-30 23:03:51
22 2010-03-30 23:03:51
23 2010-03-30 23:03:51
24 2010-03-30 23:03:51
25 2010-03-30 23:03:52
26 2010-03-30 23:03:52
27 2010-03-30 23:03:52
28 2010-03-30 23:03:52
29 2010-03-30 23:03:53
30 2010-03-30 23:03:53
31 2010-03-30 23:03:53
32 2010-03-30 23:03:53
33 2010-03-30 23:03:54
34 2010-03-30 23:03:54
35 2010-03-30 23:03:54
36 2010-03-30 23:03:54
37 2010-03-30 23:03:55
38 2010-03-30 23:03:55
39 2010-03-30 23:03:55
40 2010-03-30 23:03:55
41 2010-03-30 23:03:56
42 2010-03-30 23:03:56
43 2010-03-30 23:03:56
44 2010-03-30 23:03:56
45 2010-03-30 23:03:57
46 2010-03-30 23:03:57
47 2010-03-30 23:03:57
48 2010-03-30 23:03:57
49 2010-03-30 23:03:58
50 2010-03-30 23:03:58
51 2010-03-30 23:03:58
52 2010-03-30 23:03:58
53 2010-03-30 23:03:59
54 2010-03-30 23:03:59
55 2010-03-30 23:03:59
56 2010-03-30 23:03:59
57 2010-03-30 23:04:00
58 2010-03-30 23:04:00
59 2010-03-30 23:04:00
60 2010-03-30 23:04:00
61 2010-03-30 23:04:01
62 2010-03-30 23:04:01
63 2010-03-30 23:04:01
64 2010-03-30 23:04:01
65 2010-03-30 23:04:02
66 2010-03-30 23:04:02
67 2010-03-30 23:04:02
68 2010-03-30 23:04:02
69 2010-03-30 23:04:03
70 2010-03-30 23:04:03
71 2010-03-30 23:04:03
72 2010-03-30 23:04:03
73 2010-03-30 23:04:04
74 2010-03-30 23:04:04
75 2010-03-30 23:04:04
76 2010-03-30 23:04:04
77 2010-03-30 23:04:05
78 2010-03-30 23:04:05
79 2010-03-30 23:04:05
80 2010-03-30 23:04:05
81 2010-03-30 23:04:06
82 2010-03-30 23:04:06
83 2010-03-30 23:04:06
84 2010-03-30 23:04:06
85 2010-03-30 23:04:07
86 2010-03-30 23:04:07
87 timer stop at 2010-03-30 23:04:07
MSDN 参考 http://msdn.microsoft.com/zh-cn/library/system.threading.timer.aspx