取消长时间运行的任务Asp.net

时间:2022-09-03 20:03:28

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 WebForms。在PostBack中我创建了一个Task(任务很长时间运行)。在html页面中,我需要一个可以取消此任务的按钮。 1.我点击按钮GetResults,在服务器上运行任务2.等待一段时间后,我点击取消取消,我需要取消任务

How can I do this?

我怎样才能做到这一点?

1 个解决方案

#1


0  

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.

虽然,您可以这样做,但在ASP.NET中创建长时间运行的任务通常不是一个好主意,特别是如果它们是后台任务。

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/

Phil Haack有一篇很棒的文章 - http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/

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

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

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.

您可以考虑使用消息总线之类的东西。您还可以在数据库中考虑一个简单的TaskQueue表,您的Web应用程序将在该表中插入与您的任务相对应的状态为“待定”的记录。然后你可以有一个后台服务(比如windows服务)读取“待定”任务,并在处理过程中将它们标记为“InProgress”,并在完成时将其标记为“完成”。这样,当任务为“待定”时,您可以让用户从UI取消任务,这将简单地从数据库中删除“待处理”记录。

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

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

#1


0  

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.

虽然,您可以这样做,但在ASP.NET中创建长时间运行的任务通常不是一个好主意,特别是如果它们是后台任务。

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/

Phil Haack有一篇很棒的文章 - http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/

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

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

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.

您可以考虑使用消息总线之类的东西。您还可以在数据库中考虑一个简单的TaskQueue表,您的Web应用程序将在该表中插入与您的任务相对应的状态为“待定”的记录。然后你可以有一个后台服务(比如windows服务)读取“待定”任务,并在处理过程中将它们标记为“InProgress”,并在完成时将其标记为“完成”。这样,当任务为“待定”时,您可以让用户从UI取消任务,这将简单地从数据库中删除“待处理”记录。

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

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