2020国产成人精品视频,性做久久久久久久久,亚洲国产成人久久综合一区,亚洲影院天堂中文av色

分享

asp.net 定時(shí)器

 文成Y 2007-09-19
在使用WinForm編程中,我們可以使用定時(shí)器實(shí)現(xiàn)規(guī)定周期的定時(shí)工作進(jìn)行操作,而在Asp.Net 如何操作,卻是個(gè)問題。

而在Asp.Net Forums中有很好的例子,定時(shí)器應(yīng)用如下:
1. 更新論壇統(tǒng)計(jì)信息
2. 定時(shí)索引指定條數(shù)的帖子
3. 定時(shí)群發(fā)隊(duì)列中的郵件

Forums中對(duì)定時(shí)器的調(diào)用是放在自定義HttpModule的Init方法中(如果您沒有使用HttpModule,也可以在Globals.aspx中的Application_OnStart 中調(diào)用定時(shí)器)。

      // 定時(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 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多