不要等待长时间运行的操作 ASP.NET MVC
当用户执行某些操作(生成多个报告)时,我们会触发一些长时间运行的操作。我们不希望用户等到这些报告生成。有没有一种无需实施作业调度程序即可快速完成此任务的方法?也许使用这样的线程?或者这不安全?
public ActionResult About()
{
Thread thread = new Thread(new ThreadStart(MuchWork));
thread.Start();
return View();
}
public void MuchWork()
{
Thread.Sleep(10000);
Thread.Sleep(4000);
}
We have some long running operation firing when the user does something(generating several reports). We don't want the user to wait till those reports have been generated. Is there a quick way to accomplish this without having to implement a jobscheduler? Maybe using threads like this? Or is this not safe?
public ActionResult About()
{
Thread thread = new Thread(new ThreadStart(MuchWork));
thread.Start();
return View();
}
public void MuchWork()
{
Thread.Sleep(10000);
Thread.Sleep(4000);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅帖子 The Dangers of Implementing Recurring Back Tasks In ASP.NET by Phil Haack,其中描述了如何在使用 IRegisteredObject 接口 限制 IIS/AppPool 关闭问题。
See the post The Dangers of Implementing Recurring Background Tasks In ASP.NET by Phil Haack where he describes how to hack a psuedo-job scheduler while using the IRegisteredObject interface to limit IIS/AppPool shut-down issues.
这是不安全的。您的服务器在操作过程中崩溃了怎么办?并且用户不知道该操作从未被执行。
一般来说,工作流/持久机制会对此有所帮助,但您必须真正考虑灾难性场景的业务/用户体验案例。
It is not safe. What about your server crashing during the operation? And the user not knowing the operation never was performed.
In general, a workflow / persistent mechanism would help with that, but you have to really think about your business / user experience case for catastrphic scenarios here.
也许使用线程池或 TPL,这样您就可以检查状态并且不会被线程滥用?
我所说的“检查”是指在旋转另一个请求之前查看您已经运行了多少个请求。
我猜测操作的结果被写入某个数据库表中,以便用户可以轮询并查看状态?
maybe use the threadpool, or TPL so you can check status and not get thread-abused?
by 'check' i mean see how many of these requests you already have running before spinning another one.
im geussing that the result of the operation is written in some DB table , so the user can poll and see the status ?