Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 390ce73fe5
53 lines (46 sloc) 1.333 kb
using System;
using System.Threading.Tasks;
using System.Web.Hosting;
namespace WebBackgrounder
{
public class JobHost : IJobHost, IRegisteredObject
{
readonly object _lock = new object();
bool _shuttingDown;
public JobHost()
{
HostingEnvironment.RegisterObject(this);
}
public void Stop(bool immediate)
{
lock (_lock)
{
_shuttingDown = true;
}
HostingEnvironment.UnregisterObject(this);
}
public void DoWork(Task work)
{
if (work == null)
{
throw new ArgumentNullException("work");
}
lock (_lock)
{
if (_shuttingDown)
{
return;
}
if (work.Status == TaskStatus.Created)
{
work.Start();
}
// Need to hold the lock until the task completes.
// Later on, we should take advantage of the fact that the work is represented
// by a task. Instead of locking, we could simply have the Stop method cancel
// any pending tasks.
work.Wait();
}
}
}
}
Jump to Line
Something went wrong with that request. Please try again.