復(fù)制代碼 代碼如下:
void Application_Start(object sender, EventArgs e)
{
// 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼
//定義定時(shí)器
//1000表示1秒的意思
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
//TaskAction.SetContent 表示要調(diào)用的方法
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(TaskAction.SetContent);
myTimer.Enabled = true;
myTimer.AutoReset = true;
}
復(fù)制代碼 代碼如下:
void Session_End(object sender, EventArgs e)
{
//下面的代碼是關(guān)鍵,可解決IIS應(yīng)用程序池自動(dòng)回收的問(wèn)題
System.Threading.Thread.Sleep(1000);
//觸發(fā)事件, 寫入提示信息
TaskAction.SetContent();
//這里設(shè)置你的web地址,可以隨便指向你的任意一個(gè)aspx頁(yè)面甚至不存在的頁(yè)面,目的是要激發(fā)Application_Start
//使用您自己的URL
string url = "http://henw.cnblog.com";
System.Net.HttpWebRequest myHttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.HttpWebResponse myHttpWebResponse = (System.Net.HttpWebResponse)myHttpWebRequest.GetResponse();
System.IO.Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回寫的字節(jié)流
// 在會(huì)話結(jié)束時(shí)運(yùn)行的代碼。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式設(shè)置為 InProc 時(shí),才會(huì)引發(fā) Session_End 事件。
// 如果會(huì)話模式設(shè)置為 StateServer
// 或 SQLServer,則不會(huì)引發(fā)該事件。
}
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Timers;
/// <summary>
///Action 的摘要說(shuō)明
/// </summary>
public static class TaskAction
{
private static string content = "";
/// <summary>
/// 輸出信息存儲(chǔ)的地方.
/// </summary>
public static string Content
{
get { return TaskAction.content; }
set { TaskAction.content += "<div>" + value+"</div>"; }
}
/// <summary>
/// 定時(shí)器委托任務(wù) 調(diào)用的方法
/// </summary>
/// <param></param>
/// <param></param>
public static void SetContent(object source, ElapsedEventArgs e)
{
Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
/// <summary>
/// 應(yīng)用池回收的時(shí)候調(diào)用的方法
/// </summary>
public static void SetContent()
{
Content = "END: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
}
復(fù)制代碼 代碼如下:
<div>
<%=TaskAction.Content %>
</div>
4. 結(jié)果輸出
歡迎大家一起探討
示例源代碼下載
新聞熱點(diǎn)
疑難解答
圖片精選