Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

As you might've noticed, keeping threads running after a request returns, for processing post operational tasks (such as performing analytics on a file that was uploaded, etc) don't always complete in a web project.
There are several issues with spawning threads in the context of an ASP.NET project. Phil Haack's post explains the issues in more detail. The following classes solve the problem of IIS killing threads before they complete.

First part is the IISTaskManager:

using NLog;
using System;
using System.Threading.Tasks;
using System.Web.Hosting;
 
namespace Web.Models
{
/// <summary>
/// Static class for running background tasks in IIS.
/// Any spawned threads are registered properly with IIS
/// to ensure they finish execution.
/// </summary>
public static class IISTaskManager
{
/// <summary>
/// Runs a background task that is registered with the hosting environment
/// so it is guaranteed to finish executing.
/// </summary>
/// The lambda expression to invoke.
public static void Run(Action action)
{
new IISBackgroundTask().DoWork(action);
}
 
/// <summary>
/// Generic object for completing tasks in a background thread
/// when the request doesn't need to wait for the results
/// in the response.
/// </summary>
class IISBackgroundTask : IRegisteredObject
{
/// <summary>
/// Constructs the object and registers itself with the hosting environment.
/// </summary>
public IISBackgroundTask()
{
HostingEnvironment.RegisterObject(this);
}
 
/// <summary>
/// Called by IIS, once with  set to false
/// and then again with  set to true.
/// </summary>
void IRegisteredObject.Stop(bool immediate)
{
if (_task.IsCompleted || _task.IsCanceled || _task.IsFaulted || immediate)
{
// Task has completed or was asked to stop immediately,
// so tell the hosting environment that all work is done.
HostingEnvironment.UnregisterObject(this);
}
}
 
/// <summary>
/// Invokes the  as a Task.
/// Any exceptions are logged
/// </summary>
/// The lambda expression to invoke.
public void DoWork(Action action)
{
try
{
_task = Task.Run(action);
}
catch (AggregateException ex)
{
// Log exceptions
foreach (var innerEx in ex.InnerExceptions)
{
_logger.ErrorException(innerEx.ToString(), innerEx);
}
}
catch (Exception ex)
{
_logger.ErrorException(ex.ToString(), ex);
}
}
 
private Task _task;
private static Logger _logger = LogManager.GetCurrentClassLogger();
}
}
}

Usage:

IISTaskManager.Run(() =&gt;
  {
    //Run any piece of code which needs to run in a new thread
  }
);