开发者

backgroundworker blocking MVC controller action

开发者 https://www.devze.com 2023-01-19 05:30 出处:网络
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 met

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, DoWor开发者_StackOverflow社区kEventArgs 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


You can just start new thread:

public ActionResult MyAction()
{
    var workingThread = new Thread(Foo);
    workingThread.Start();

    return View("Thankyou");
}

void Foo()
{
    Thread.Sleep(10000);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消