简体   繁体   English

在 web 表单上取消任务的最佳方法是什么

[英]What is the best way to cancel a task on a web form

I have a long task that runs, and can take up to 15 minutes, I allow the user to only run one at a time.我有一个运行很长的任务,最多可能需要 15 分钟,我允许用户一次只运行一个。 This all works well but I can't get my results and have a cancel button.这一切都很好,但我无法得到我的结果并且有一个取消按钮。 Currently I have the cancel button write a file, and if that file is present during a loop, the task completes and self deletes.目前我有取消按钮写一个文件,如果该文件在循环中存在,任务完成并自我删除。 However, I can't get the results of the task to do anything, so errors aren't reported or anything.但是,我无法获得执行任何任务的结果,因此不会报告错误或任何其他内容。 What is your recommendation on doing this?您对此有何建议?

Libraries needed for below (iirc)以下所需的库 (iirc)

using System
using System.Threading
using System.Threading.Tasks

Here is a rough example of my execution code.这是我的执行代码的粗略示例。

protected void Execute_Click(object sender, EventArgs e)
{
    Task taskExecute = new Task(()=>
    {
        // 15 minute task
        // I also look for cancelation file during loop periodically.
    }
    taskExecute.Start();

    // If i wait for the task, it freezes the webpage so this is commented out
    // taskExecute.Wait();
}

here is my cancelation code:这是我的取消代码:

protected void Cancel_Click(object sender, EventArgs e)
{
    //code to write file that long script looks for...
}

If you're wondering, the code is called via <asp:button onclick="Execute_Click"> calls.如果您想知道,该代码是通过<asp:button onclick="Execute_Click">调用调用的。 etc... ETC...

Also, I can click the cancel button if I refresh the page, but that defeats the purpose, as the responses in my results box won't be visible.此外,如果我刷新页面,我可以单击取消按钮,但这违背了目的,因为我的结果框中的响应将不可见。

What you're looking for can be accomplished with Azure Durable Functions and add in SignalR/Websockets if you're looking to track state in real-time without polling.如果您希望在不进行轮询的情况下实时跟踪 state,则可以使用 Azure Durable Functions 并添加 SignalR/Websockets 来完成您正在寻找的内容。

Calls to durable functions return with an ID that you can check on intermittently using a built-in state management endpoint on your function. State objects support custom data as well.对持久函数的调用返回一个 ID,您可以使用 function 上的内置 state 管理端点间歇性地检查该 ID。State 对象也支持自定义数据。 This loose coupling allows you to execute and forget calls to the API without blocking any threads.这种松散耦合允许您在不阻塞任何线程的情况下执行并忘记对 API 的调用。 Each call returns a unique ID, so you can run multiple instances in parallel, and easily track them.每次调用都会返回一个唯一的 ID,因此您可以并行运行多个实例,并轻松跟踪它们。 Connect your web client and web service to a websockets channel (SignalR), and you can send real-time updates or events from your webservice TO your webclient, including a "process completed" event that you can use to trigger the download/render of output/logs/etc...将您的 web 客户端和 web 服务连接到 websockets 通道 (SignalR),您可以将实时更新或事件从您的 web 服务发送到您的 web 客户端,包括可用于触发下载/渲染的“处理完成”事件输出/日志/等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM