取消长时间运行的任务Asp.net [英] Cancelling long running tasks Asp.net

查看:73
本文介绍了取消长时间运行的任务Asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET WebForms.在回发中,我创建一个任务(任务运行时间很长).在html页面中,我需要一个可以取消此任务的按钮.1.我单击在服务器上运行任务的GetResults按钮2.等待一会后,我单击取消"按钮,我需要任务将被取消

I'm using ASP.NET WebForms. In PostBack I create a Task(task is very long running). In the html page I need a button that can cancel this task. 1. I click button GetResults that Run a task on the server 2. After some waiting I click button Cancel and I need the task will be cancelled

我该怎么做?

推荐答案

尽管可以做到,但是在ASP.NET中创建长期运行的任务通常不是一个好主意,尤其是当它们是后台任务时.

Though, you can do it, it is generally not a good idea to create a long running task within ASP.NET, especially if they are background tasks.

Phil Haack对此有很不错的文章-

Phil Haack has a great article on this - http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/

这是关于该主题的另一篇好文章- http://blog.stephencleary.com/2012/12/returning-early-from-aspnet-requests.html

Here is another good article on that topic - http://blog.stephencleary.com/2012/12/returning-early-from-aspnet-requests.html

为此,您可以考虑使用诸如消息总线之类的东西.您还可以考虑数据库中的一个简单TaskQueue表,您的Web应用程序将在其中插入与您的任务相对应的状态为待决"的记录.然后,您可以拥有一个后台服务(例如Windows服务),该服务读取待处理"任务并将其在处理过程中标记为进行中",并在完成时将其标记为完成".这样,当任务为待处理"时,您可以让用户从UI取消任务,这将简单地从数据库中删除待处理"记录.

You can consider things like message bus for this. You can also consider a simple TaskQueue table in the database to which your web application will insert a record corresponding to your task with status "Pending". Then you can have a background service (like windows service) that reads "Pending" tasks and marks them as "InProgress" during processing and "Complete" when its done. That way while the task is "Pending", you can have the user cancel the task from UI, which will simply delete the "Pending" record from the database.

另一种选择是使用这样的解决方案- http://hangfire.io/

Another option is to use a solution such as this - http://hangfire.io/

这篇关于取消长时间运行的任务Asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆