后台工作者阻止 MVC 控制器操作
我想在新线程/异步中从 ASP.NET MVC 控制器操作运行一些代码。我不关心响应,我想在异步方法在后台运行时触发并忘记并向用户返回一个视图。我认为 BackgroundWorker
类适合这个?
public ActionResult MyAction()
{
var backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += Foo;
backgroundWorker.RunWorkerAsync();
return View("Thankyou");
}
void Foo(object sender, DoWorkEventArgs e)
{
Thread.Sleep(10000);
}
为什么这段代码会导致返回视图之前有 10 秒的延迟?为什么视图没有立即返回?
更重要的是,我需要做什么才能使这项工作成功?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以开始新线程:
You can just start new thread: