c# – 在长时间服务器进程中如何显示信息量的实时进度数据

c# – 在长时间服务器进程中如何显示信息量的实时进度数据,第1张

概述我有一个漫长的过程可能需要1个小时. 这个过程包括从年到年的许多步骤.我的主要问题是: 如何在进程期间向终端用户提供一个信息丰富的实时进度,而不仅仅是虚拟加载栏. int index = Convert.ToInt32(e.CommandArgument); bool done = false; int res = -1; i 我有一个漫长的过程可能需要1个小时.

这个过程包括从年到年的许多步骤.我的主要问题是:

如何在进程期间向终端用户提供一个信息丰富的实时进度,而不仅仅是虚拟加载栏.

int index = Convert.ToInt32(e.CommandArgument);            bool done = false;            int res = -1;            int fromVal = int.Parse(gv_balance.Rows[index].Cells[0].Text);            int toval = int.Parse(gv_balance.Rows[index].Cells[1].Text);            int finMonth = 1;            int finYear = 0;            int EndServ = 0;            int calcYear = int.Parse(gv_balance.Rows[index].Cells[2].Text);            int total;            total = ((toval - fromVal) + 1);            string msg = string.Empty;            int confirm = Balance.GetConfirmState(calcYear);            if (confirm == 0)            {                RadProgressContext progress = RadProgressContext.Current;                progress.Speed = "N/A";                finYear = fromVal;                for (int i = fromVal; i <= toval; i++)                {                    decimal ratio;                    //Load New Employees                    if (toval - fromVal > 0)                    {                        ratio = ((decimal)toval - i) / (toval - fromVal) * 100;                    }                    else                    {                        ratio = ((decimal)toval - i) / 1 * 100;                    }                    progress.PrimaryTotal = total;                    progress.PrimaryValue = total;                    progress.PrimaryPercent = 100;                    progress.SecondaryTotal = 100; // total;                    progress.SecondaryValue = ratio;//i ;                    progress.SecondaryPercent = ratio; //i;                    progress.CurrentoperationText = "Step " + i.ToString();                    if (!Response.IsClIEntConnected)                    {                        //Cancel button was clicked or the browser was closed,so stop processing                        break;                    }                    progress.TimeEstimated = (toval - i) * 100;                    //Stall the current thread for 0.1 seconds                    System.Threading.Thread.Sleep(100);                    EndServ = i + 1;                    if (i == fromVal)                    {                           //--->STEP1                        //Load intial data                         int intial = Balance.PrepareIntialData(calcYear);                        //--->STEP2                        res = Balance.CalcEndServed(calcYear,EndServ - 1,6,30);                    }                     //--->STEP3                    int newEmps = Balance.PrepareNewEmployees(calcYear,i);                    for (int j = 0; j < 2; j++)                    {                        if (j == 0)                        {                            finMonth = 7;                            finYear = i;                        }                        else                        {                            finMonth = 1;                            finYear = i + 1;                        }                        //--->STEP4                        int promotion1 = Balance.PreparePromotionFirst(finYear,finMonth,calcYear);                         //--->STEP5                        int promotion2 = Balance.PreparePromotionSecond(finYear,calcYear);                         //--->STEP6                        int appointment1 = Balance.PrepareAppointmentFirst(finYear,calcYear);                         //--->STEP7                        int appointment2 = Balance.PrepareAppointmentSecond(finYear,calcYear);                        //--->STEP8                        int bonus = Balance.PrepareBonus(finMonth,finYear,calcYear);                         //--->STEP9                        int salary = Balance.PrepareSalary(finYear,calcYear);                     (((CheckBox)gv_balance.Rows[index].Cells[3].FindControl("chk_redirect")).Checked == true)                        {                           //--->STEP9                            int acco = Balance.PrepareFinanceAccount(finYear,calcYear);                        }                    }                    //--->STEP10                    res = Balance.CalcEndServed(calcYear,EndServ,30);                    Balance.CalcStudy(calcYear);                    UpdateProgressContext();                    if (res < 0)                    {                        success_lb.Visible = false;                        error_lb.Visible = true;                        error_lb.Text = "ERROR";                    }                    else                    {                        done = true;                        success_lb.Visible = true;                        error_lb.Visible = false;                        success_lb.Text = "Success";                    }                }            }

我想显示当前步骤,例如:
(促销1)in —> 1-2018以及估计时间之外的整个过程的百分比.

解决方法 要使用signalR来报告一个很长的任务的进度,你可以这样做(这只是一个例子来说明它是如何工作的):

服务器部分

我们首先映射SignalR.

public class Startup{   public voID Configuration(IAppBuilder app)   {       // Any connection or hub wire up and configuration should go here       app.MapSignalR();   }}

我们创建一个Hub类(不要忘了安装signalr包):

(如果要向所有连接的用户或特定的用户组报告进度,请查看:http://www.asp.net/signalr/overview/guide-to-the-api/working-with-groups)

在给定的例子中,它仅向Start函数的调用者报告进度.

public class MyHub : Hub{         public voID Start(string arg)    {        Task.Run(() =>        {            AVeryLongTask();        });    }    //simulate a long task    voID AVeryLongTask()    {        for (int i = 0; i < 10000; i++)        {            Thread.Sleep(100);                          ClIEnts.Caller.ReportProgress("AVeryLongTask",i * 100 / 10000);        }    }}

客户部分

在HTML中,您必须添加以下引用:

<!--Script references. --><!--Reference the jquery library. --><script src="Scripts/jquery-1.6.4.min.Js"></script><!--Reference the SignalR library. --><script src="/Scripts/jquery.signalR-2.0.0.Js"></script><!--Reference the autogenerated SignalR hub script. --><script src="/signalr/hubs"></script>

现在的Js部分是从中心获得进步:

$(function() {   // Declare a proxy to reference the hub.   var hub = $.connection.myHub;   // Create a function that the hub can call to report progress.   hub.clIEnt.reportProgress = function(functionname,progress) {     $('#progression').append('<li><strong>' + progress + '</strong>:&nbsp;&nbsp;' + functionname + '</li>');   };   // Start the connection.   $.connection.hub.start().done(function() {     $('#startlongprocess').click(function() {      //start the long process       hub.server.start("arg");       alert("started");     });   }); });

用于进度和开始按钮的HTML容器:

<div >       <input type="button" ID="startlongprocess" value="Send" />   <ul ID="progression"></ul></div>

如果您需要更多的解释,请不要犹豫.

(我的例子是基于这个来自signalr团队的http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr)

总结

以上是内存溢出为你收集整理的c# – 在长时间服务器进程中如何显示信息量的实时进度数据全部内容,希望文章能够帮你解决c# – 在长时间服务器进程中如何显示信息量的实时进度数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1260089.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存