热门关键字:
会员中心
用户名:
密 码:
验证码:
 
推荐文档
 
当前位置:主页>编程学习>.Net源码及技巧>ASP.NET>文章内容
ASP.NET - asp.net forums中定时器的应用
来源:网络 作者:JingJingDe.net 发布时间:2008-07-27  

在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
//
/**//// %26lt;summary%26gt;
/// Initializes the HttpModule and performs the wireup of all application
/// events.
/// %26lt;/summary%26gt;
/// %26lt;param name="application"%26gt;Application the module is being run for%26lt;/param%26gt;
public void Init(HttpApplication application) {

// Wire-up application events
//
// 略去其他代码

ForumConfiguration forumConfig = ForumConfiguration.GetConfig();

// 如果使用定时器并且定时器还没初始化
if( forumConfig != null
%26amp;%26amp; forumConfig.IsBackgroundThreadingDisabled == false ) {
if (emailTimer == null)
// 新建定时器
// 新建一个TimerCallback委托,具体要执行的方法在ScheduledWorkCallbackEmailInterval中
emailTimer = new Timer(new TimerCallback(ScheduledWorkCallbackEmailInterval), application.Context, EmailInterval, EmailInterval);

if( forumConfig.IsIndexingDisabled == false
%26amp;%26amp; statsTimer == null ) {
statsTimer = new Timer(new TimerCallback(ScheduledWorkCallbackStatsInterval), application.Context, StatsInterval, StatsInterval);
}
}
}

/**//// %26lt;summary%26gt;
/// 释放定时器
/// %26lt;/summary%26gt;
public void Dispose() {
statsTimer = null;
emailTimer = null;
}

Timer Callbacks
    #region Timer Callbacks
/**//// %26lt;summary%26gt;
/// 定时发送队列中待发送的邮件
/// %26lt;/summary%26gt;
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 );
}
}

/**//// %26lt;summary%26gt;
/// 定时索引帖子和定时更新论坛统计信息
/// %26lt;/summary%26gt;
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小时将最新的数据列表生成静态的文本。


 
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论:
 

网站版权信息

为我最爱的静静而奋斗!为我最爱的多多而努力!by.莫名奇妙

联系站长:QQ:44611446