首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ASP.Net MVC中长时间运行的服务器调用的进度条

ASP.Net MVC中长时间运行的服务器调用的进度条
EN

Stack Overflow用户
提问于 2014-12-05 18:16:02
回答 1查看 60.8K关注 0票数 23

我只想在长时间运行服务器调用时创建一个进度条。当控制器正在执行长时间运行的工作时,我无法创建对控制器的ajax post请求。

我想创建一个额外的操作来获取当前长时间运行任务的实际语句。我尝试在ajax请求中创建轮询,然后我可以从服务器端返回状态,并将其显示在客户端进度条中。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-17 21:06:04

要做到这一点,正确且最简单的方法是使用SignalR。请下载https://www.nuget.org/packages/Microsoft.AspNet.SignalR/2.1.2格式的微软SignalR

在项目路径中名为hubs的单独文件夹中创建一个hub类,并将两个类文件添加到hubs文件夹中

Startup.cs

代码语言:javascript
复制
using Owin;
using Microsoft.Owin;
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}

ProgressHub.cs

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace RealTimeProgressBar
{
    public class ProgressHub : Hub
    {

        public string msg = "Initializing and Preparing...";
        public int count = 1;

        public static void SendMessage(string msg , int count)
        {
            var message = "Process completed for " + msg;
            var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
            hubContext.Clients.All.sendMessage(string.Format(message), count);
        }

        public void GetCountAndMessage()
        {
            Clients.Caller.sendMessage(string.Format(msg), count);
        }
    }
}

在控制器中,

代码语言:javascript
复制
// assemblies
using Microsoft.AspNet.SignalR;
using RealTimeProgressBar;   


//call this method inside your working action
ProgressHub.SendMessage("initializing and preparing",  2);

在视图中,

代码语言:javascript
复制
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.1.2.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.--> 
<script type="text/javascript">
  $(document).ready(function () {

    $("#progressBar").kendoProgressBar({
        min: 0,
        max: 100,
        type: "percent",
    });
});

function StartInvoicing()
{
    var progressNotifier = $.connection.progressHub;

    // client-side sendMessage function that will be called from the server-side
    progressNotifier.client.sendMessage = function (message, count) {
        // update progress
        UpdateProgress(message, count);
        //alert(message);
    };

    // establish the connection to the server and start server-side operation
    $.connection.hub.start().done(function () {
        // call the method CallLongOperation defined in the Hub
        progressNotifier.server.getCountAndMessage();
    });
}

// Update the progress bar 
function UpdateProgress(message, count) {
    var result = $("#result");
    result.html(message);
    $("#progressBar").data("kendoProgressBar").value(count);
}
</script>

有关更多详细信息,请参阅google帮助下的一些现有文章

票数 49
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27313550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档