这个简易方法,取经自:http://www.qumiao.com
以下是Global.asax文件的内容。
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Timers" %>
<%@ Import Namespace="System.Net" %>
<script runat="server">
protected void Application_Start(object sender, EventArgs e) {
//每5秒执行一次周期任务
Timer myTimer = new Timer(5000);
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
myTimer.Enabled = true;
myTimer.AutoReset = true;
}
protected void Application_End(object sender, EventArgs e) {
//下面的代码是关键,可解决IIS应用程序池自动回收的问题
//这里设置你的web地址,可以随便指向你的任意一个aspx页面甚至不存在的页面,
//目的是要激发Application_Start
System.Threading.Thread.Sleep(1000);
WebRequest.Create("http://localhost/").GetResponse();
}
void myTimer_Elapsed(object source, ElapsedEventArgs e) {
try { CycleTask(); } catch {}
}
void CycleTask() {
//在这里写你需要执行的周期性任务
}
</script>
本文介绍了一个简单的ASP.NET应用中实现周期性任务的方法。通过在Global.asax文件中配置定时器,每隔5秒触发一个事件来执行指定的任务。此外,还提供了解决IIS应用程序池自动回收导致定时任务中断的问题方案。
617

被折叠的 条评论
为什么被折叠?



