当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 如何保证web app中的Send Email线程稳定性

ASP.NET
Asp.net Ajax--Calendar控件使用
让ASP.NET程序自动为URL加上超级链接
ASP.NET2.0MasterPage技巧总结
asp.net读取数据库乱码的解决完全方案
asp.net中生成缩略图并添加版权
ASP.Net用MD5和SHA1加密的几种方法
asp.net客户端回调功能的实现机制
ASP.NET2.0中控件的简单异步回调
用在JavaScript的RequestHelper
用Java发送图文并茂的HTML邮件
基于.NET平台的分层架构实战(一) 综述
基于.NET平台的分层架构实战(二)需求分析与数据库设计
基于.NET平台的分层架构实战(三)架构概要设计
基于.NET平台的分层架构实战(四)实体类的设计与实现
近期的几个ASP.NET开发经验总结和收集
ASP.NET中的状态管理
asp.net基础知识介绍
对数据访问层第一种实现(Acc+SQL)的重构
.NET初学者推荐课程 asp.net错误代码大全
在.net中如何利用数据工厂实现多数据库的操作

ASP.NET 中的 如何保证web app中的Send Email线程稳定性


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 83 ::
收藏到网摘: n/a

在asp.netforums2或者cs系统中,每过几分种会发送一次email,但是如果在执行时,数据库服务器重启。

会导致SendEmail线程死掉。

参考代码:
privatevoidScheduledWorkCallbackEmailInterval(objectsender)
{
try{
//suspendthetimerwhileweprocessemails
emailTimer.Change(System.Threading.Timeout.Infinite,EmailInterval);

//Sendemails
//
Emails.SendQueuedEmails((HttpContext)sender);


//Updateanonymoususers
//
Users.UpdateAnonymousUsers((HttpContext)sender);

}
catch(Exceptione){
ForumExceptionfe=newForumException(ForumExceptionType.EmailUnableToSend,"ScheduledWorkerThreadfailed.",e);
fe.Log();
}
finally{
emailTimer.Change(EmailInterval,EmailInterval);
}
}

事实上,代码:emailTimer.Change(System.Threading.Timeout.Infinite,EmailInterval);

不够强壮,理论上讲,如果在执行中出错,会执行:

finally{
emailTimer.Change(EmailInterval,EmailInterval);
}


但,事实上,如果是数据库服务器重启,则可以让timer线程永远死掉。

手工解决方法:重启这个webapp

或者改写代码:emailTimer.Change(System.Threading.Timeout.Infinite,EmailInterval);

为:emailTimer.Change(EmailInterval*2,EmailInterval);

可以解决


其他参考:

msdn:

System.Theading.Timer.ChangeAPI: