// 定時(shí)器 static Timer statsTimer; static Timer emailTimer; // 定時(shí)間隔 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(); // 如果使用定時(shí)器并且定時(shí)器還沒初始化 if( forumConfig != null && forumConfig.IsBackgroundThreadingDisabled == false ) { if (emailTimer == null) // 新建定時(shí)器 // 新建一個(gè)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> /// 釋放定時(shí)器 /// </summary> public void Dispose() { statsTimer = null; emailTimer = null; } Timer Callbacks#region Timer Callbacks /**//// <summary> /// 定時(shí)發(fā)送隊(duì)列中待發(fā)送的郵件 /// </summary> private void ScheduledWorkCallbackEmailInterval (object sender) { try { // 當(dāng)處理郵件時(shí)暫停定時(shí)器 emailTimer.Change( System.Threading.Timeout.Infinite, EmailInterval ); // 發(fā)送隊(duì)列中的郵件 // 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 { // 重新啟動(dòng)定時(shí)器 emailTimer.Change( EmailInterval, EmailInterval ); } } /**//// <summary> /// 定時(shí)索引帖子和定時(shí)更新論壇統(tǒng)計(jì)信息 /// </summary> private void ScheduledWorkCallbackStatsInterval(object sender) { try { // 休眠定時(shí)器 statsTimer.Change( System.Threading.Timeout.Infinite, StatsInterval ); // 每次索引100篇帖子 // Search.IndexPosts( (HttpContext) sender, 100); // 更新論壇統(tǒng)計(jì)信息 SiteStatistics.LoadSiteStatistics( (HttpContext) sender, true, 1 ); } catch( Exception e ) { ForumException fe = new ForumException( ForumExceptionType.UnknownError, "Failure performing scheduled statistics maintenance.", e ); fe.Log(); } finally { // 喚醒定時(shí)器 statsTimer.Change( StatsInterval, StatsInterval); } } #endregion |
聯(lián)系客服