在B/S系統中引入定時器的功能
發表于:2007-07-14來源:作者:點擊數:
標簽:
過去的一些使用ASP技術 開發 的B/S系統中,需要系統定時執行一些方法時一直都 找不到好的 解決方案 (如果有,那一定是我淺薄了,我們討論討論)。 現在在ASP。NET中可以使用自定義實現IHttpModule接口的類來加載一個定時器。 public class OilIHttpModule :
過去的一些使用ASP技術開發的B/S系統中,需要系統定時執行一些方法時一直都
找不到好的解決方案(如果有,那一定是我淺薄了,我們討論討論)。
現在在ASP。NET中可以使用自定義實現IHttpModule接口的類來加載一個定時器。
public class OilIHttpModule : IHttpModule
{
public static Timer analyseTimer;//分析數據的定時器
static int intLastTrialInfo_id;//最后分析的ID
static long intAnalyseInterval= 10000;//間隔的時間
public OilIHttpModule()
{ }
public String ModuleName
{
get { return "OilModule"; }
}
///初始化模型
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler
(this.Application_BeginRequest));
//增加處理請求時觸發的事件
if(intLastTrialInfo_id==0)
{ //獲取最后分析的
trialInfo_id intLastTrialInfo_id =
globalMethod.getLastAnalyseTrialInfo_id();
}
//判斷Timer是否存在,如果沒有則實例化
if(analyseTimer==null)
analyseTimer = new Timer(new TimerCallback(analyseData),null,
intAnalyseInterval,intAnalyseInterval);
}
private void Application_BeginRequest(Object source, EventArgs e)
{
//null
// HttpApplication application = (HttpApplication)source;
// application.Response.Write(intLastTrialInfo_id.ToString());
}
///要定時執行的程序片段
private void analyseData(object obj)
{
///很重要,可以防止定時器被重新生成
analyseTimer.Change( System.Threading.Timeout.Infinite,
intAnalyseInterval );
// StatsInterval
int intTrialInfo_idAfterUpdate;
intTrialInfo_idAfterUpdate =
globalMethod.AnalyseTrialFromTrialInfo_id(intLastTrialInfo_id);
if(intTrialInfo_idAfterUpdate > intLastTrialInfo_id)
{
intLastTrialInfo_id = intTrialInfo_idAfterUpdate;
}
}
public void Dispose()
{
analyseTimer = null;
}
原文轉自:http://www.kjueaiud.com