asp.net 定時器

來源:互聯網
上載者:User
在使用WinForm編程中,我們可以使用定時器實現規定周期的定時工作進行操作,而在Asp.Net 如何操作,卻是個問題。

而在Asp.Net Forums中有很好的例子,定時器應用如下:
1. 更新論壇統計資訊
2. 定時索引指定條數的文章
3. 定時群發隊列中的郵件

Forums中對定時器的調用是放在自訂HttpModule的Init方法中(如果您沒有使用HttpModule,也可以在Globals.aspx中的Application_OnStart 中調用定時器)。

      // 定時器 
        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委託,具體要執行的方法在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> 
        /// 定時發送隊列中待發送的郵件 
        /// </summary> 
        private void ScheduledWorkCallbackEmailInterval (object sender) { 
            try { 
                // 當處理郵件時暫停定時器 
                emailTimer.Change( System.Threading.Timeout.Infinite, EmailInterval ); 
 
                // 發送隊列中的郵件 
                // 
                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> 
        /// 定時索引文章和定時更新論壇統計資訊 
        /// </summary> 
        private void ScheduledWorkCallbackStatsInterval(object sender) { 
            try { 
                // 休眠定時器 
                statsTimer.Change( System.Threading.Timeout.Infinite, StatsInterval ); 
 
                // 每次索引100篇文章 
                // 
                Search.IndexPosts( (HttpContext) sender, 100); 
 
                // 更新論壇統計資訊 
                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 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.