Asp.Net:executionTimeout +并行线程

时间:2013-04-08 10:29:15

标签: asp.net multithreading parallel-processing

在网页上,我启动并行进程以执行长时间运行的任务,然后发送电子邮件以跟踪其“输出”。 但该线程有时似乎在发送电子邮件之前结束(电子邮件是我唯一的电影),我认为这可能与超时有关。

这里是代码:

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();

你认为这与某些超时有关吗?我可以在不允许常规请求永久运行的情况下进行更改吗?

1 个答案:

答案 0 :(得分:1)

你不应该在网络应用程序中开始后台工作 - 如果应用程序池被回收,你的后台工作将因极端的偏见而中止。

Phil Haack在这里写了一篇关于这个主题的博客:

The Dangers of Implementing Recurring Background Tasks In ASP.NET

建议的解决方案是将此工作传递给单独的Windows服务。