欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【如何保证web app中的Send Email线程稳定性】,下面是详细的分享!
如何保证web app中的Send Email线程稳定性
在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:
以上所分享的是关于如何保证web app中的Send Email线程稳定性,下面是编辑为你推荐的有价值的用户互动:
相关问题:如何保证web app中的Send Email线程稳定性
答:会导致SendEmail线程死掉。参考代码:emailTimer.Change(System.Threading.Timeout.Infinite,EmailInterval);//Sendemails//Emails.SendQueuedEmails((HttpContext)sender);//Updateanonymoususers//Users.UpdateAnonymousUsers((HttpContext)se... >>详细
相关问题:java线程锁的问题,如何保证web应用中的synchroniz...
答:为什么不用 synchronized(ObjClass.class) 直接锁在类上呢? 而且这个你可以用AtomicInteger做 更加方便,就是自增嘛 >>详细
相关问题:java线程锁的问题,如何保证web应用中的synchroniz...
答:把这个类配置成单例模式,用代码来完善一下。设计模式里面有。只有一个私有构造方法。自己去找,网上很多 >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
