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 qumiao.com
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
//定義定時(shí)器
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()
{
//在這里寫(xiě)你需要執行的任務(wù)
}
protected void Application_End(object sender, EventArgs e)
{
Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Application End!");
//下面的代碼是關(guān)鍵,可解決IIS應用程序池自動(dòng)回收的問(wèn)題
Thread.Sleep(1000);
//這里設置你的web地址,可以隨便指向你的任意一個(gè)aspx頁(yè)面甚至不存在的頁(yè)面,目的是要激發(fā)Application_Start
string url = http://www.abc.com
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回寫(xiě)的字節流
}
}
}
原理:Global.asax 可以是asp.net中應用程序或會(huì )話(huà)事件處理程序,我們用到了Application_Start(應用程序開(kāi)始事件)和Application_End(應用程序結束事件)。當應用程序開(kāi)始時(shí),啟動(dòng)一個(gè)定時(shí)器,用來(lái)定時(shí)執行任務(wù)YourTask()方法,這個(gè)方法里面可以寫(xiě)上需要調用的邏輯代碼,可以是單線(xiàn)程和多線(xiàn)程。當應用程序結束時(shí),如IIS的應用程序池回收,讓asp.net去訪(fǎng)問(wèn)當前的這個(gè)web地址。這里需要訪(fǎng)問(wèn)一個(gè)aspx頁(yè)面,這樣就可以重新激活應用程序。Log類(lèi)是一個(gè)記錄日志的一個(gè)類(lèi),下面是測試時(shí)生成的日志信息:
================================================================
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!
從日志中發(fā)現,當手動(dòng)回收IIS的應用程序池之后,計劃任務(wù)還在執行,說(shuō)明我們的目的達到了。
如果將Application_End中的代碼注釋掉,會(huì )發(fā)現Application End之后,計劃任務(wù)停止工作了,如下:
================================================================
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!
局限性:可以解決應用程序池自動(dòng)或者手動(dòng)回收,但是無(wú)法解決IIS重啟或者web服務(wù)器重啟的問(wèn)題,當然這種情況出現的時(shí)候不多,而且如果有人訪(fǎng)問(wèn)你的網(wǎng)站的時(shí)候,又會(huì )自動(dòng)激活計劃任務(wù)了。
方案二、
<%@ Application Language="C#" %>
<%@ import Namespace="System.IO" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// 在應用程序啟動(dòng)時(shí)運行的代碼
System.Timers.Timer myTimer = new System.Timers.Timer(10000);
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
myTimer.Interval = 10000;
myTimer.Enabled = true;
}
void Application_End(object sender, EventArgs e)
{
// 在應用程序關(guān)閉時(shí)運行的代碼
}
void Application_Error(object sender, EventArgs e)
{
// 在出現未處理的錯誤時(shí)運行的代碼
}
void Session_Start(object sender, EventArgs e)
{
// 在新會(huì )話(huà)啟動(dòng)時(shí)運行的代碼
}
void Session_End(object sender, EventArgs e)
{
// 在會(huì )話(huà)結束時(shí)運行的代碼。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式設置為
// InProc 時(shí),才會(huì )引發(fā) Session_End 事件。如果會(huì )話(huà)模式設置為 StateServer
// 或 SQLServer,則不會(huì )引發(fā)該事件。
}
private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
//間隔時(shí)間執行某動(dòng)作
//指定日志文件的目錄
string fileLogPath = AppDomain.CurrentDomain.BaseDirectory + "SystemLog";
string fileLogName = "SoftPrj_CN_" + DateTime.Now.ToLongDateString() + "_log.txt";
//定義文件信息對象
FileInfo finfo = new FileInfo(fileLogPath + fileLogName);
//創(chuàng )建只寫(xiě)文件流
using (FileStream fs = finfo.OpenWrite())
{
//根據上面創(chuàng )建的文件流創(chuàng )建寫(xiě)數據流
StreamWriter strwriter = new StreamWriter(fs);
//設置寫(xiě)數據流的起始位置為文件流的末尾
strwriter.BaseStream.Seek(0, SeekOrigin.End);
//寫(xiě)入錯誤發(fā)生時(shí)間www.qichepeijian.com
strwriter.WriteLine("發(fā)生時(shí)間: " + DateTime.Now.ToString());
//寫(xiě)入日志內容并換行
//strwriter.WriteLine("錯誤內容: " + message);
strwriter.WriteLine("錯誤內容: ");
//寫(xiě)入間隔符
strwriter.WriteLine("---------------------------------------------");
strwriter.WriteLine();
//清空緩沖區內容,并把緩沖區內容寫(xiě)入基礎流
strwriter.Flush();
//關(guān)閉寫(xiě)數據流
strwriter.Close();
fs.Close();
}
}
</script>
方案三、
<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Threading" %>
<script RunAt="server">
string LogPath;
Thread thread;
void WriteLog()
{
while (true)
{
StreamWriter sw = new StreamWriter(LogPath, true, Encoding.UTF8);
sw.WriteLine(thread.Name + ":" + DateTime.Now.ToString());
sw.Close();
Thread.CurrentThread.Join(1000 * 10);//阻止10秒
}
}
void Application_Start(object sender, EventArgs e)
{
LogPath = HttpContext.Current.Server.MapPath("log.txt"); //在應用程序啟動(dòng)時(shí)運行的代碼
thread = new Thread(new ThreadStart(WriteLog));
thread.Name = "寫(xiě)登錄日志線(xiàn)程";
thread.Start();
}
void Application_End(object sender, EventArgs e)
{
// 在應用程序關(guān)閉時(shí)運行的代碼
}
void Application_Error(object sender, EventArgs e)
{
// 在出現未處理的錯誤時(shí)運行的代碼
}
void Session_Start(object sender, EventArgs e)
{
// 在新會(huì )話(huà)啟動(dòng)時(shí)運行的代碼
}
void Session_End(object sender, EventArgs e)
{
// 在會(huì )話(huà)結束時(shí)運行的代碼。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式設置為
// InProc 時(shí),才會(huì )引發(fā) Session_End 事件。如果會(huì )話(huà)模式設置為 StateServer
// 或 SQLServer,則不會(huì )引發(fā)該事件。
}
</script>
聯(lián)系客服