I want to run some code from an ASP.NET MVC controller action in a new thread / asynchronously. I do not need an answer, I want to start and forget and return the view to the user while the asynchronous method is running in the background. I thought a class BackgroundWorkeris suitable 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 a 10 second delay before returning the View? Why isnt View returned instantly?
More details, what do I need to do to make this work?
thank
source
share