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 中调用定时器)。

None.gif          //  定时器
None.gif
         static  Timer statsTimer;
None.gif        
static  Timer emailTimer;
None.gif
None.gif        
//  定时间隔
None.gif
         private   long  EmailInterval  =  ForumConfiguration.GetConfig().ThreadIntervalEmail  *   60000 ;
None.gif        
private   long  StatsInterval  =  ForumConfiguration.GetConfig().ThreadIntervalStats  *   60000 ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
public  String ModuleName  dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn "ForumsHttpModule"; } 
ExpandedBlockEnd.gif        }
    
None.gif
None.gif
None.gif        
//  *********************************************************************
None.gif        
//   ForumsHttpModule
None.gif        
//
ExpandedBlockStart.gifContractedBlock.gif
         /**/ /// <summary>
InBlock.gif        
/// Initializes the HttpModule and performs the wireup of all application
InBlock.gif        
/// events.
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name="application">Application the module is being run for</param>

ExpandedBlockStart.gif ContractedBlock.gif          public   void  Init(HttpApplication application)  dot.gif
InBlock.gif
InBlock.gif            
// Wire-up application events
InBlock.gif            
//
InBlock.gif            
// 略去其他代码dot.gif
InBlock.gif
            
InBlock.gif            ForumConfiguration forumConfig 
= ForumConfiguration.GetConfig();
InBlock.gif
InBlock.gif            
// 如果使用定时器并且定时器还没初始化
InBlock.gif
            if( forumConfig != null
ExpandedSubBlockStart.gifContractedSubBlock.gif            
&&  forumConfig.IsBackgroundThreadingDisabled == false ) dot.gif{
InBlock.gif                
if (emailTimer == null)
InBlock.gif                    
// 新建定时器
InBlock.gif                    
// 新建一个TimerCallback委托,具体要执行的方法在ScheduledWorkCallbackEmailInterval中
InBlock.gif
                    emailTimer = new Timer(new TimerCallback(ScheduledWorkCallbackEmailInterval), application.Context, EmailInterval, EmailInterval);
InBlock.gif
InBlock.gif                
if( forumConfig.IsIndexingDisabled == false 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
&&    statsTimer == null ) dot.gif{
InBlock.gif                    statsTimer 
= new Timer(new TimerCallback(ScheduledWorkCallbackStatsInterval), application.Context, StatsInterval, StatsInterval);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        }

None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 释放定时器
ExpandedBlockEnd.gif        
/// </summary>

ExpandedBlockStart.gif ContractedBlock.gif          public   void  Dispose()  dot.gif {
InBlock.gif            statsTimer 
= null;
InBlock.gif            emailTimer 
= null;
ExpandedBlockEnd.gif        }

None.gif
ContractedBlock.gifExpandedBlockStart.gif        
Timer Callbacks #region Timer Callbacks
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 定时发送队列中待发送的邮件
ExpandedSubBlockEnd.gif        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        private void ScheduledWorkCallbackEmailInterval (object sender) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                
// 当处理邮件时暂停定时器
InBlock.gif
                emailTimer.Change( System.Threading.Timeout.Infinite, EmailInterval );
InBlock.gif
InBlock.gif                
// 发送队列中的邮件
InBlock.gif                
//
InBlock.gif
                Emails.SendQueuedEmails( (HttpContext) sender);
InBlock.gif
InBlock.gif
InBlock.gif                
// 更新匿名用户
InBlock.gif                
//
InBlock.gif
                Users.UpdateAnonymousUsers( (HttpContext) sender);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch( Exception e ) dot.gif{
InBlock.gif                ForumException fe 
= new ForumException( ForumExceptionType.EmailUnableToSend, "Scheduled Worker Thread failed.", e );
InBlock.gif                fe.Log();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
finally dot.gif{
InBlock.gif                
// 重新启动定时器
InBlock.gif
                emailTimer.Change( EmailInterval, EmailInterval );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 定时索引帖子和定时更新论坛统计信息
ExpandedSubBlockEnd.gif        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        private void ScheduledWorkCallbackStatsInterval(object sender) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                
// 休眠定时器
InBlock.gif
                statsTimer.Change( System.Threading.Timeout.Infinite, StatsInterval );
InBlock.gif
InBlock.gif                
// 每次索引100篇帖子
InBlock.gif                
//
InBlock.gif
                Search.IndexPosts( (HttpContext) sender, 100);
InBlock.gif
InBlock.gif                
// 更新论坛统计信息
InBlock.gif
                SiteStatistics.LoadSiteStatistics( (HttpContext) sender, true1 );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch( Exception e ) dot.gif{
InBlock.gif                ForumException fe 
= new ForumException( ForumExceptionType.UnknownError, "Failure performing scheduled statistics maintenance.", e );
InBlock.gif                fe.Log();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
finally dot.gif{
InBlock.gif                
// 唤醒定时器
InBlock.gif
                statsTimer.Change( StatsInterval, StatsInterval);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

其实稍加改进就可以应用到我们自己的项目中,例如前不久刚做一个项目,因为数据量过于庞大,每次从数据库取非常慢,然后改成使用定时器,每隔12小时将最新的数据列表生成静态的文本。

转载于:https://www.cnblogs.com/gaohades/archive/2006/01/24/322626.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值