需要一个具有用户反馈的 ASP.NET MVC 长期运行过程 [英] Need an ASP.NET MVC long running process with user feedback

查看:17
本文介绍了需要一个具有用户反馈的 ASP.NET MVC 长期运行过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在我的项目中创建一个控制器,用于交付可能非常复杂的报告.因此,他们可能需要相对较长的时间,进度条肯定会帮助用户了解事情正在进展中.该报告将通过 AJAX 请求启动,其想法是定期 JSON 请求将获取状态并更新进度条.

I've been trying to create a controller in my project for delivering what could turn out to be quite complex reports. As a result they can take a relatively long time and a progress bar would certainly help users to know that things are progressing. The report will be kicked off via an AJAX request, with the idea being that periodic JSON requests will get the status and update the progress bar.

我一直在试验 AsyncController,因为这似乎是一种在不占用资源的情况下运行长进程的好方法,但它似乎没有给我任何检查进度的方法(并且似乎进一步阻止JSON 请求,我还没有发现原因).在那之后,我尝试将进度存储在控制器上的静态变量中并从中读取状态 - 但老实说,这一切似乎都有些笨拙!

I've been experimenting with the AsyncController as that seems to be a nice way of running long processes without tying up resources, but it doesn't appear to give me any way of checking on the progress (and seems to block further JSON requests and I haven't discovered why yet). After that I've tried resorting to storing progress in a static variable on the controller and reading the status from that - but to be honest that all seems a bit hacky!

非常感谢所有建议!

推荐答案

这是我写的一个示例,您可以尝试:

Here's a sample I wrote that you could try:

控制器:

public class HomeController : AsyncController
{
    public ActionResult Index()
    {
        return View();
    }

    public void SomeTaskAsync(int id)
    {
        AsyncManager.OutstandingOperations.Increment();
        Task.Factory.StartNew(taskId =>
        {
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(200);
                HttpContext.Application["task" + taskId] = i;
            }
            var result = "result";
            AsyncManager.OutstandingOperations.Decrement();
            AsyncManager.Parameters["result"] = result;
            return result;
        }, id);
    }

    public ActionResult SomeTaskCompleted(string result)
    {
        return Content(result, "text/plain");
    }

    public ActionResult SomeTaskProgress(int id)
    {
        return Json(new
        {
            Progress = HttpContext.Application["task" + id]
        }, JsonRequestBehavior.AllowGet);
    }
}

Index() 视图:

Index() View:

<script type="text/javascript">
$(function () {
    var taskId = 543;
    $.get('/home/sometask', { id: taskId }, function (result) {
        window.clearInterval(intervalId);
        $('#result').html(result);
    });

    var intervalId = window.setInterval(function () {
        $.getJSON('/home/sometaskprogress', { id: taskId }, function (json) {
            $('#progress').html(json.Progress + '%');
        });
    }, 5000);
});
</script>

<div id="progress"></div>
<div id="result"></div>

这个想法是启动一个异步操作,该操作将使用 HttpContext.Application 报告进度,这意味着每个任务必须有一个唯一的 id.然后在客户端我们启动任务,然后发送多个 AJAX 请求(每 5 秒)来更新进度.您可以调整参数以适应您的场景.进一步的改进是添加异常处理.

The idea is to start an asynchronous operation that will report the progress using HttpContext.Application meaning that each task must have an unique id. Then on the client side we start the task and then send multiple AJAX requests (every 5s) to update the progress. You may tweak the parameters to adjust to your scenario. Further improvement would be to add exception handling.

这篇关于需要一个具有用户反馈的 ASP.NET MVC 长期运行过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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