在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小时将最新的数据列表生成静态的文本。
新闻热点
疑难解答
图片精选