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

首頁 > 編程 > .NET > 正文

asp.net forums中定時器的應用

2024-07-10 13:06:59
字體:
來源:轉載
供稿:網友
  • 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。
  • 在asp.net中使用定時器,破寶之前已有blog寫過《在 asp.net 中使用計時器(timer)》,這里主要針對asp.net forums來說一下其具體實現。

    在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
    其實稍加改進就可以應用到我們自己的項目中,例如前不久剛做一個項目,因為數據量過于龐大,每次從數據庫取非常慢,然后改成使用定時器,每隔12小時將最新的數據列表生成靜態的文本。

     

     

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 图们市| 滨州市| 阿城市| 山东| 米脂县| 彭山县| 密云县| 湘乡市| 梧州市| 绥化市| 蚌埠市| 清流县| 伊金霍洛旗| 东乌珠穆沁旗| 墨江| 民勤县| 张掖市| 淄博市| 屏山县| 嘉义市| 高安市| 即墨市| 桐梓县| 屏南县| 临洮县| 静海县| 汽车| 清苑县| 且末县| 长乐市| 社旗县| 桐柏县| 旌德县| 吉首市| 昆明市| 淄博市| 乐陵市| 廊坊市| 融水| 蓝田县| 乾安县|