栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

正确,最简单的方法是使用SignalR。请在https://www.nuget.org/packages/Microsoft.AspNet.SignalR/2.1.2中下载Microsoft
SignalR

在项目路径的单独文件夹中创建中心类,该中心称为hubs,将两个类文件添加到hubs文件夹中

Startup.cs

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

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);        }    }}

在控制器中

// assembliesusing Microsoft.AspNet.SignalR;using RealTimeProgressBar;//call this method inside your working actionProgressHub.SendMessage("initializing and preparing",  2);

鉴于

<!--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的一些现有文章。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/386101.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号