// 定時器 static Timer statsTimer; static Timer emailTimer; // 定時間隔 private long EmailInterval = ForumConfiguration.GetConfig().ThreadIntervalEmail * 60000; private long StatsInterval = ForumConfiguration.GetConfig().ThreadIntervalStats * 60000; public String ModuleName { get { return "ForumsHttpModule"; } } // ********************************************************************* // ForumsHttpModule // /**//// <summary> /// Initializes the HttpModule and performs the wireup of all application /// events. /// </summary> /// <param name="application">Application the module is being run for</param> public void Init(HttpApplication application) { // Wire-up application events // // 略去其他代碼 ForumConfiguration forumConfig = ForumConfiguration.GetConfig(); // 如果使用定時器并且定時器還沒初始化 if( forumConfig != null && forumConfig.IsBackgroundThreadingDisabled == false ) { if (emailTimer == null) // 新建定時器 // 新建一個TimerCallback委托,具體要執(zhí)行的方法在ScheduledWorkCallbackEmailInterval中 emailTimer = new Timer(new TimerCallback(ScheduledWorkCallbackEmailInterval), application.Context, EmailInterval, EmailInterval); if( forumConfig.IsIndexingDisabled == false && statsTimer == null ) { statsTimer = new Timer(new TimerCallback(ScheduledWorkCallbackStatsInterval), application.Context, StatsInterval, StatsInterval); } } } /**//// <summary> /// 釋放定時器 /// </summary> public void Dispose() { statsTimer = null; emailTimer = null; } Timer Callbacks#region Timer Callbacks /**//// <summary> /// 定時發(fā)送隊列中待發(fā)送的郵件 /// </summary> private void ScheduledWorkCallbackEmailInterval (object sender) { try { // 當處理郵件時暫停定時器 emailTimer.Change( System.Threading.Timeout.Infinite, EmailInterval ); // 發(fā)送隊列中的郵件 // Emails.SendQueuedEmails( (HttpContext) sender); // 更新匿名用戶 // Users.UpdateAnonymousUsers( (HttpContext) sender); } catch( Exception e ) { ForumException fe = new ForumException( ForumExceptionType.EmailUnableToSend, "Scheduled Worker Thread failed.", e ); fe.Log(); } finally { // 重新啟動定時器 emailTimer.Change( EmailInterval, EmailInterval ); } } /**//// <summary> /// 定時索引帖子和定時更新論壇統(tǒng)計信息 /// </summary> private void ScheduledWorkCallbackStatsInterval(object sender) { try { // 休眠定時器 statsTimer.Change( System.Threading.Timeout.Infinite, StatsInterval ); // 每次索引100篇帖子 // Search.IndexPosts( (HttpContext) sender, 100); // 更新論壇統(tǒng)計信息 SiteStatistics.LoadSiteStatistics( (HttpContext) sender, true, 1 ); } catch( Exception e ) { ForumException fe = new ForumException( ForumExceptionType.UnknownError, "Failure performing scheduled statistics maintenance.", e ); fe.Log(); } finally { // 喚醒定時器 statsTimer.Change( StatsInterval, StatsInterval); } } #endregion
|