title: ASP.NET long running task. Thread is being aborted exception
body: <p>An ASP.NET 3.5 webapp has to start several tasks that takes hours to complete. For obvious reasons the pages which starts these tasks cannot wait for them to finish nor will anyone want to wait that long to get a response, so the tasks must be asynchronous.</p>
  
  <p>There is a Helper class to handle all of these long running tasks. The main method that schedules and executes these tasks is currently the following:</p>
  
  <pre><code>public static bool ScheduleTask(TaskDescriptor task, Action action)
  {
      bool notAlreadyRunning = TasksAsync.TryAdd(task);
      if (notAlreadyRunning)
      {
          Thread worker = null;
          worker = new Thread(() =&gt; 
          {
              try { action(); }
              catch(Exception e)
              {
                  Log.LogException(e, "Worker");
              }
              TasksAsync.RemoveTask(task);
              workers.Remove(worker);
          });
          workers.Add(worker);
          worker.Start();
      }
      return notAlreadyRunning;
  }
  </code></pre>
  
  <p>On earlier implementations we've used the <code>ThreadPool.QueueUserWorkItem</code> approach but the result has always been the same: after aprox. 20-30 mins a Thread was being aborted exception is thrown.</p>
  
  <p>Does anyone know why is this happening? or how can it be prevented?</p>
  
  <p><strong>More Info:</strong></p>
  
  <ul>
  <li>IIS standard configuration.</li>
  <li>Tasks could be anything, querys to a database and/or IO operations etc.</li>
  </ul>
  
  <p><strong>UPDATE: Decisions</strong></p>
  
  <p>Thank you all for your responses. Now I don´t know which question to mark as answer. All of them are valid and are possible solutions to this problem. Will wait for today and mark as answer the answer with the most up votes, in case of a draw I will choose the first shown answer, typically they are ordered by most relevance.</p>
  
  <p>For anyone who want´s to know the solution I choose, again due to time restrictions, was to change the IIS recycling configuration, But what I consider to be the ideal solution, based on my research and of course the answers below, is to create a "Worker Service" and use a communication solution between the ASP.NET App and the new "Worker Service" to coordinate the long running work to be done.</p>