日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

asp.net 定時器

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

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

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

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

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約