根据不同需求,在web开发中,有时候我们可能要定时的执行一下任务,比如定时发布网站中的文章,这时我们就需要做一个定时执行更新的操作,但由于web的http是无状态的连接,如何才能时时进行任务的更新呢?方法还是有的!
原理:Global.asax 可以是asp.net中应用程序或会话事件处理程序,我们用到了Application_Start(应用程序开始事件)和Application_End(应用程序结束事件)。当应用程序开始时,启动一个定时器,用来定时执行任务YourTask()方法,这个方法里面可以写上需要调用的逻辑代码,可以是单线程和多线程。当应用程序结束时,如IIS的应用程序池回收,让asp.net去访问当前的这个web地址。这里需要访问一个aspx页面,这样就可以重新激活应用程序。
局限性:可以解决应用程序池自动或者手动回收,但是无法解决IIS重启或者web服务器重启的问题,当然这种情况出现的时候不多,而且如果有人访问你的网站的时候,又会自动激活计划任务了。
C#代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.SessionState; using System.Timers; using System.Net; using System.IO; using System.Text; using System.Threading; namespace cutepig.net { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { //定义定时器 System.Timers.Timer myTimer = new System.Timers.Timer(5000); myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed); myTimer.Enabled = true; myTimer.AutoReset = true; } void myTimer_Elapsed(object source, ElapsedEventArgs e) { try { Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":AutoTask is Working!"); YourTask(); } catch (Exception ee) { Log.SaveException(ee); } } void YourTask() { //在这里写你需要执行的任务 } protected void Application_End(object sender, EventArgs e) { Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Application End!"); //下面的代码是关键,可解决IIS应用程序池自动回收的问题 Thread.Sleep(1000); //这里设置你的web地址,可以随便指向你的任意一个aspx页面甚至不存在的页面,目的是要激发Application_Start string url = "https://home.cutepig.net"; HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流 } } } |
VB.NET代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
Imports System.Web.SessionState Imports System.Timers Imports System.Threading Imports System.Net Imports System.Net.WebRequestMethods Imports System.IO Public Class Global_asax Inherits System.Web.HttpApplication Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) ' 应用程序启动时激发 Dim SetTime As Integer = 60000 '设定定时时间 单位:秒 Dim myTimer As System.Timers.Timer = New System.Timers.Timer(SetTime) AddHandler myTimer.Elapsed, New ElapsedEventHandler(AddressOf myTimer_Elapsed) myTimer.Enabled = True myTimer.AutoReset = True End Sub Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) ' 会话启动时激发 End Sub Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) ' 每个请求开始时激发 End Sub Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs) ' 尝试验证用户身份时激发 End Sub Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ' 发生错误时激发 End Sub Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) ' 会话结束时激发 End Sub Private Sub myTimer_Elapsed(ByVal source As Object, ByVal e As ElapsedEventArgs) Try Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & ":AutoTask is Working!") YourTask() Catch ee As Exception Log.SaveException(ee) End Try End Sub Private Sub YourTask() '在这里写你需要执行的任务 End Sub Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) ' 应用程序结束时激发 Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & ":Application End!") Thread.Sleep(1000) Dim url As String = "Default.aspx" Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest) Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse) Dim receiveStream As Stream = myHttpWebResponse.GetResponseStream() End Sub End Class |
浏览量: 5