国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net 定時器

2024-07-10 13:06:23
字體:
來源:轉載
供稿:網友
在使用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 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙坪坝区| 名山县| 高邑县| 都江堰市| 永顺县| 富阳市| 龙江县| 博野县| 新巴尔虎右旗| 贵州省| 平塘县| 镇巴县| 简阳市| 马鞍山市| 阳谷县| 法库县| 江达县| 南部县| 浙江省| 于都县| 宜城市| 柳河县| 泰来县| 烟台市| 渑池县| 河西区| 襄樊市| 梁山县| 湘潭市| 湘潭县| 宁海县| 苏尼特右旗| 枣庄市| 饶河县| 沙坪坝区| 来凤县| 清徐县| 松原市| 阿合奇县| 集安市| 大同县|