在一个网页上,我启动一个并行进程来完成一个长时间运行的任务,然后发送一封电子邮件来跟踪它的“输出”。但有时线程似乎在发送电子邮件之前就结束了(电子邮件是我拥有的唯一轨迹),我认为这可能与超时有关。
代码如下:
var thread = new Thread(() =>
{
var cli = Factory.GetClient(callBack);
FedDBService.ReturnMessage msg;
try
{
msg = cli.SendFedCards(xmls.ToArray());
}
catch (Exception exe)
{
msg = new FedDBService.ReturnMessage();
msg.Error = exe.Message;
}
finally
{
cli.Close();
completion.Finished = true;
}
message = String.Format(@"{0}Ended: {1: HH:mm} {2} {3}", message, DateTime.Now,
msg.Error.Trim(' ', '\n', '\r', '\t') == "" ?
"Withouth errors" : "With errors:"
, msg.Error);
try
{
golf.classes.Mailing.Mail.sendMail(Club.ClubEmail, email, null,
"***@***.com", message, "**Subject**", false);
// this method works right
}
finally
{
Thread.Sleep(5 * 60 * 1000);
RemoveCompletion(guid);
}
});
thread.Start();你认为这和超时有关吗?我可以在不允许通常的请求永远运行的情况下改变它吗?
发布于 2013-04-08 19:13:42
你不应该在web应用程序中开始后台工作-如果应用程序池被回收,你的后台工作将被极端的偏见中止。
Phil Haack在这里写了一篇关于这个话题的博客:
The Dangers of Implementing Recurring Background Tasks In ASP.NET
建议的解决方案是将此工作传递给单独的Windows服务。
https://stackoverflow.com/questions/15876843
复制相似问题