asp.net forums中定时器的应用
原文:http://blog.joycode.com/dotey/archive/2004/12/20/41438.aspx 
 // 定时器
        // 定时器 
 static Timer statsTimer;
        static Timer statsTimer; 
 static Timer emailTimer;
        static Timer emailTimer; 
 
 
 // 定时间隔
        // 定时间隔 
 private long EmailInterval = ForumConfiguration.GetConfig().ThreadIntervalEmail * 60000;
        private long EmailInterval = ForumConfiguration.GetConfig().ThreadIntervalEmail * 60000; 
 private long StatsInterval = ForumConfiguration.GetConfig().ThreadIntervalStats * 60000;
        private long StatsInterval = ForumConfiguration.GetConfig().ThreadIntervalStats * 60000; 
 
 
 public String ModuleName {
        public String ModuleName {  
 get { return "ForumsHttpModule"; }
            get { return "ForumsHttpModule"; }  
 }
        }     
 
 
 
 
 // *********************************************************************
        // ********************************************************************* 
 //  ForumsHttpModule
        //  ForumsHttpModule 
 //
        // 
 /// <summary>
        /// <summary> 
 /// Initializes the HttpModule and performs the wireup of all application
        /// Initializes the HttpModule and performs the wireup of all application 
 /// events.
        /// events. 
 /// </summary>
        /// </summary> 
 /// <param name="application">Application the module is being run for</param>
        /// <param name="application">Application the module is being run for</param> 
 public void Init(HttpApplication application) {
        public void Init(HttpApplication application) {  
 
 
 // Wire-up application events
            // Wire-up application events 
 //
            // 
 // 略去其他代码
            // 略去其他代码 
 
 
             
 ForumConfiguration forumConfig = ForumConfiguration.GetConfig();
            ForumConfiguration forumConfig = ForumConfiguration.GetConfig(); 
 
 
 // 如果使用定时器并且定时器还没初始化
            // 如果使用定时器并且定时器还没初始化 
 if( forumConfig != null
            if( forumConfig != null 
 &&  forumConfig.IsBackgroundThreadingDisabled == false ) {
            &&  forumConfig.IsBackgroundThreadingDisabled == false ) { 
 if (emailTimer == null)
                if (emailTimer == null) 
 // 新建定时器
                    // 新建定时器 
 // 新建一个TimerCallback委托,具体要执行的方法在ScheduledWorkCallbackEmailInterval中
                    // 新建一个TimerCallback委托,具体要执行的方法在ScheduledWorkCallbackEmailInterval中 
 emailTimer = new Timer(new TimerCallback(ScheduledWorkCallbackEmailInterval), application.Context, EmailInterval, EmailInterval);
                    emailTimer = new Timer(new TimerCallback(ScheduledWorkCallbackEmailInterval), application.Context, EmailInterval, EmailInterval); 
 
 
 if( forumConfig.IsIndexingDisabled == false
                if( forumConfig.IsIndexingDisabled == false  
 &&    statsTimer == null ) {
                &&    statsTimer == null ) { 
 statsTimer = new Timer(new TimerCallback(ScheduledWorkCallbackStatsInterval), application.Context, StatsInterval, StatsInterval);
                    statsTimer = new Timer(new TimerCallback(ScheduledWorkCallbackStatsInterval), application.Context, StatsInterval, StatsInterval); 
 }
            } 
 }
        } 
 }
        } 
 
 
 /// <summary>
        /// <summary> 
 /// 释放定时器
        /// 释放定时器 
 /// </summary>
        /// </summary> 
 public void Dispose() {
        public void Dispose() { 
 statsTimer = null;
            statsTimer = null; 
 emailTimer = null;
            emailTimer = null; 
 }
        } 
 
 
 Timer Callbacks
        Timer Callbacks 
asp.net forums中定时器的应用
在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 statsTimer;  static Timer emailTimer;
        static Timer emailTimer;  
  // 定时间隔
        // 定时间隔  private long EmailInterval = ForumConfiguration.GetConfig().ThreadIntervalEmail * 60000;
        private long EmailInterval = ForumConfiguration.GetConfig().ThreadIntervalEmail * 60000;  private long StatsInterval = ForumConfiguration.GetConfig().ThreadIntervalStats * 60000;
        private long StatsInterval = ForumConfiguration.GetConfig().ThreadIntervalStats * 60000;  
  public String ModuleName {
        public String ModuleName {   get { return "ForumsHttpModule"; }
            get { return "ForumsHttpModule"; }   }
        }      
  
  // *********************************************************************
        // *********************************************************************  //  ForumsHttpModule
        //  ForumsHttpModule  //
        //  /// <summary>
        /// <summary>  /// Initializes the HttpModule and performs the wireup of all application
        /// Initializes the HttpModule and performs the wireup of all application  /// events.
        /// events.  /// </summary>
        /// </summary>  /// <param name="application">Application the module is being run for</param>
        /// <param name="application">Application the module is being run for</param>  public void Init(HttpApplication application) {
        public void Init(HttpApplication application) {   
  // Wire-up application events
            // Wire-up application events  //
            //  // 略去其他代码
            // 略去其他代码 
  
              ForumConfiguration forumConfig = ForumConfiguration.GetConfig();
            ForumConfiguration forumConfig = ForumConfiguration.GetConfig();  
  // 如果使用定时器并且定时器还没初始化
            // 如果使用定时器并且定时器还没初始化  if( forumConfig != null
            if( forumConfig != null  &&  forumConfig.IsBackgroundThreadingDisabled == false ) {
            &&  forumConfig.IsBackgroundThreadingDisabled == false ) {  if (emailTimer == null)
                if (emailTimer == null)  // 新建定时器
                    // 新建定时器  // 新建一个TimerCallback委托,具体要执行的方法在ScheduledWorkCallbackEmailInterval中
                    // 新建一个TimerCallback委托,具体要执行的方法在ScheduledWorkCallbackEmailInterval中  emailTimer = new Timer(new TimerCallback(ScheduledWorkCallbackEmailInterval), application.Context, EmailInterval, EmailInterval);
                    emailTimer = new Timer(new TimerCallback(ScheduledWorkCallbackEmailInterval), application.Context, EmailInterval, EmailInterval);  
  if( forumConfig.IsIndexingDisabled == false
                if( forumConfig.IsIndexingDisabled == false   &&    statsTimer == null ) {
                &&    statsTimer == null ) {  statsTimer = new Timer(new TimerCallback(ScheduledWorkCallbackStatsInterval), application.Context, StatsInterval, StatsInterval);
                    statsTimer = new Timer(new TimerCallback(ScheduledWorkCallbackStatsInterval), application.Context, StatsInterval, StatsInterval);  }
            }  }
        }  }
        }  
  /// <summary>
        /// <summary>  /// 释放定时器
        /// 释放定时器  /// </summary>
        /// </summary>  public void Dispose() {
        public void Dispose() {  statsTimer = null;
            statsTimer = null;  emailTimer = null;
            emailTimer = null;  }
        }  
  Timer Callbacks
        Timer Callbacks 其实稍加改进就可以应用到我们自己的项目中,例如前不久刚做一个项目,因为数据量过于庞大,每次从数据库取非常慢,然后改成使用定时器,每隔12小时将最新的数据列表生成静态的文本。
BTW: 有技术八股文之嫌哦:P
 
                    
                
 
             
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号