Serge
Serge

Reputation: 6692

Asp.Net : executionTimeout + parallel thread

On a webpage, I launche a parallel process to do a long running task then send an email to keep track of its "output". But the thread sometimes seems to end before sending the email (the email is the only track Ihave) and I think it might be related to a timeout.

Here the code:

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

Do you think it's related to some timeout? And can I change it withouth allowing usual requests to run forever?

Upvotes: 0

Views: 414

Answers (1)

Nick Butler
Nick Butler

Reputation: 24383

You shouldn't start background work in a web app - if the App Pool is recycled, your background work will be aborted with extreme predjudice.

Phil Haack wrote a blog about this topic here:

The Dangers of Implementing Recurring Background Tasks In ASP.NET

The recommended solution is to pass this work to a seperate Windows Service.

Upvotes: 1

Related Questions