I want to run some code from an ASP.NET MVC controller action in a new thread/asynchronously. I don't care about the response, I want to fire and forget and return the user a view while the async method runs in the background. I thought the BackgroundWorker
class was appropriate for this?
public ActionResult MyAction()
{
var backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += Foo;
backgroundWorker.RunWorkerAsync();
return View("Thankyou");
}
void Foo(object sender, DoWorkEventArgs e)
{
Thread.Sleep(10000);
}
Why does this code cause there to be a 10 second delay before returning the View? Why isnt the View returned instantly?
More to the point, what do I need to do to make this work?
Thanks