asp.net定时执行任务的三个方法

根据不同需求,在web开发中,有时候我们可能要定时的执行一下任务,比如定时发布网站中的文章,这时我们就需要做一个定时执行更新的操作,但由于web的http是无状态的连接,如何才能时时进行任务的更新呢?方法还是有的,看看下面三种方法吧!

方法一:

  1using System;  
  2      
  3    using System.Data;  
  4      
  5    using System.Configuration;  
  6      
  7    using System.Collections;  
  8      
  9    using System.Web;  
 10      
 11    using System.Web.Security;  
 12      
 13    using System.Web.SessionState;  
 14      
 15    using System.Timers;  
 16      
 17    using System.Net;  
 18      
 19    using System.IO;  
 20      
 21    using System.Text;  
 22      
 23    using System.Threading;  
 24      
 25    namespace qumiao.com  
 26      
 27    {  
 28      
 29    public class Global : System.Web.HttpApplication  
 30      
 31    {  
 32      
 33    protected void Application_Start(object sender, EventArgs e)  
 34      
 35    {  
 36      
 37    //定义定时器  
 38      
 39    System.Timers.Timer myTimer = new System.Timers.Timer(5000);  
 40      
 41    myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);  
 42      
 43    myTimer.Enabled = true;  
 44      
 45    myTimer.AutoReset = true;  
 46      
 47    }  
 48      
 49    void myTimer_Elapsed(object source, ElapsedEventArgs e)  
 50      
 51    {  
 52      
 53    try 
 54      
 55    {  
 56      
 57    Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":AutoTask is Working!");  
 58      
 59    YourTask();  
 60      
 61    }  
 62      
 63    catch (Exception ee)  
 64      
 65    {  
 66      
 67    Log.SaveException(ee);  
 68      
 69    }  
 70      
 71    }  
 72      
 73    void YourTask()  
 74      
 75    {  
 76      
 77    //在这里写你需要执行的任务  
 78      
 79    }  
 80      
 81    protected void Application_End(object sender, EventArgs e)  
 82      
 83    {  
 84      
 85    Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Application End!");  
 86      
 87    //下面的代码是关键,可解决IIS应用程序池自动回收的问题  
 88      
 89    Thread.Sleep(1000);  
 90      
 91    //这里设置你的web地址,可以随便指向你的任意一个aspx页面甚至不存在的页面,目的是要激发Application_Start  
 92      
 93    string url = http://www.shaoqun.com  
 94      
 95    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);  
 96      
 97    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();  
 98      
 99    Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流  
100      
101    }  
102      
103    }  
104      
105    }

原理:Global.asax 可以是asp.net中应用程序或会话事件处理程序,我们用到了Application_Start(应用程序开始事件)和Application_End(应用程序结束事件)。当应用程序开始时,启动一个定时器,用来定时执行任务YourTask()方法,这个方法里面可以写上需要调用的逻辑代码,可以是单线程和多线程。当应用程序结束时,如IIS的应用程序池回收,让asp.net去访问当前的这个web地址。这里需要访问一个aspx页面,这样就可以重新激活应用程序。Log类是一个记录日志的一个类,下面是测试时生成的日志信息:

================================================================

2008-10-30 17:46:10:AutoTask is Working!

2008-10-30 17:46:15:AutoTask is Working!

2008-10-30 17:46:20:AutoTask is Working!

2008-10-30 17:46:23:Application End!

2008-10-30 17:46:29:AutoTask is Working!

2008-10-30 17:46:34:AutoTask is Working!

从日志中发现,当手动回收IIS的应用程序池之后,计划任务还在执行,说明我们的目的达到了。

如果将Application_End中的代码注释掉,会发现Application End之后,计划任务停止工作了,如下:

================================================================

2008-10-30 18:01:34:AutoTask is Working!

2008-10-30 18:01:39:AutoTask is Working!

2008-10-30 18:01:44:AutoTask is Working!

2008-10-30 18:01:46:Application End!

局限性:可以解决应用程序池自动或者手动回收,但是无法解决IIS重启或者web服务器重启的问题,当然这种情况出现的时候不多,而且如果有人访问你的网站的时候,又会自动激活计划任务了。

方法二:

  1<%@ Application Language="C#" %>  
  2       
  3     <%@ import Namespace="System.IO" %>  
  4       
  5     <script runat="server">  
  6       
  7     void Application_Start(object sender, EventArgs e)  
  8       
  9     {  
 10       
 11     // 在应用程序启动时运行的代码  
 12       
 13     System.Timers.Timer myTimer = new System.Timers.Timer(10000);  
 14       
 15     myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);  
 16       
 17     myTimer.Interval = 10000;  
 18       
 19     myTimer.Enabled = true;  
 20       
 21     }  
 22       
 23     void Application_End(object sender, EventArgs e)  
 24       
 25     {  
 26       
 27     //  在应用程序关闭时运行的代码  
 28       
 29     }  
 30       
 31     void Application_Error(object sender, EventArgs e)  
 32       
 33     {  
 34       
 35     // 在出现未处理的错误时运行的代码  
 36       
 37     }  
 38       
 39     void Session_Start(object sender, EventArgs e)  
 40       
 41     {  
 42       
 43     // 在新会话启动时运行的代码  
 44       
 45     }  
 46       
 47     void Session_End(object sender, EventArgs e)  
 48       
 49     {  
 50       
 51     // 在会话结束时运行的代码。  
 52       
 53     // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为  
 54       
 55     // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer  
 56       
 57     // 或 SQLServer,则不会引发该事件。  
 58       
 59     }  
 60       
 61     private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)  
 62       
 63     {  
 64       
 65     //间隔时间执行某动作  
 66       
 67     //指定日志文件的目录  
 68       
 69     string fileLogPath = AppDomain.CurrentDomain.BaseDirectory + "SystemLog";  
 70       
 71     string fileLogName = "SoftPrj_CN_" + DateTime.Now.ToLongDateString() + "_log.txt";  
 72       
 73     //定义文件信息对象  
 74       
 75     FileInfo finfo = new FileInfo(fileLogPath + fileLogName);  
 76       
 77     //创建只写文件流  
 78       
 79     using (FileStream fs = finfo.OpenWrite())  
 80       
 81     {  
 82       
 83     //根据上面创建的文件流创建写数据流  
 84       
 85     StreamWriter strwriter = new StreamWriter(fs);  
 86       
 87     //设置写数据流的起始位置为文件流的末尾  
 88       
 89     strwriter.BaseStream.Seek(0, SeekOrigin.End);  
 90       
 91     //写入错误发生时间  
 92       
 93     strwriter.WriteLine("发生时间: " + DateTime.Now.ToString());  
 94       
 95     //写入日志内容并换行  
 96       
 97     //strwriter.WriteLine("错误内容: " + message);  
 98       
 99     strwriter.WriteLine("错误内容: ");  
100       
101     //写入间隔符  
102       
103     strwriter.WriteLine("---------------------------------------------");  
104       
105     strwriter.WriteLine();  
106       
107     //清空缓冲区内容,并把缓冲区内容写入基础流  
108       
109     strwriter.Flush();  
110       
111     //关闭写数据流  
112       
113     strwriter.Close();  
114       
115     fs.Close();  
116       
117     }  
118       
119     }  
120       
121     </script>  

方法三:

 1<%@ Application Language="C#" %>  
 2  
 3<%@ Import Namespace="System.IO" %>  
 4  
 5<%@ Import Namespace="System.Threading" %>  
 6  
 7<script RunAt="server">  
 8  
 9string LogPath;  
10  
11Thread thread;  
12  
13void WriteLog()  
14  
15{  
16  
17while (true)  
18  
19{  
20  
21StreamWriter sw = new StreamWriter(LogPath, true, Encoding.UTF8);  
22  
23sw.WriteLine(thread.Name + ":" + DateTime.Now.ToString());  
24  
25sw.Close();  
26  
27Thread.CurrentThread.Join(1000 * 10);//阻止10秒  
28  
29}  
30  
31}  
32  
33void Application_Start(object sender, EventArgs e)  
34  
35{  
36  
37LogPath = HttpContext.Current.Server.MapPath("log.txt");        //在应用程序启动时运行的代码  
38  
39thread = new Thread(new ThreadStart(WriteLog));  
40  
41thread.Name = "写登录日志线程";  
42  
43thread.Start();  
44  
45}  
46  
47void Application_End(object sender, EventArgs e)  
48  
49{  
50  
51//  在应用程序关闭时运行的代码  
52  
53}  
54  
55void Application_Error(object sender, EventArgs e)  
56  
57{  
58  
59// 在出现未处理的错误时运行的代码  
60  
61}  
62  
63void Session_Start(object sender, EventArgs e)  
64  
65{  
66  
67// 在新会话启动时运行的代码  
68  
69}  
70  
71void Session_End(object sender, EventArgs e)  
72  
73{  
74  
75// 在会话结束时运行的代码。  
76  
77// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为  
78  
79// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer  
80  
81// 或 SQLServer,则不会引发该事件。  
82  
83}  
84  
85</script>