需要用户反馈的ASP.NET MVC长时间运行进程 [英] Need an ASP.NET MVC long running process with user feedback

查看:108
本文介绍了需要用户反馈的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);
    }
}

查看:

<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天全站免登陆