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

前端之家收集整理的这篇文章主要介绍了c# – 在长时间服务器进程中如何显示信息量的实时进度数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个漫长的过程可能需要1个小时.

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

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

  1. int index = Convert.ToInt32(e.CommandArgument);
  2. bool done = false;
  3. int res = -1;
  4. int fromVal = int.Parse(gv_balance.Rows[index].Cells[0].Text);
  5. int toVal = int.Parse(gv_balance.Rows[index].Cells[1].Text);
  6. int finMonth = 1;
  7. int finYear = 0;
  8. int EndServ = 0;
  9. int calcYear = int.Parse(gv_balance.Rows[index].Cells[2].Text);
  10. int total;
  11. total = ((toVal - fromVal) + 1);
  12. string msg = string.Empty;
  13.  
  14. int confirm = Balance.GetConfirmState(calcYear);
  15. if (confirm == 0)
  16. {
  17. RadProgressContext progress = RadProgressContext.Current;
  18. progress.Speed = "N/A";
  19.  
  20. finYear = fromVal;
  21.  
  22. for (int i = fromVal; i <= toVal; i++)
  23. {
  24. decimal ratio;
  25. //Load New Employees
  26. if (toVal - fromVal > 0)
  27. {
  28. ratio = ((decimal)toVal - i) / (toVal - fromVal) * 100;
  29. }
  30. else
  31. {
  32. ratio = ((decimal)toVal - i) / 1 * 100;
  33. }
  34. progress.PrimaryTotal = total;
  35. progress.PrimaryValue = total;
  36. progress.PrimaryPercent = 100;
  37.  
  38. progress.SecondaryTotal = 100; // total;
  39. progress.SecondaryValue = ratio;//i ;
  40. progress.SecondaryPercent = ratio; //i;
  41.  
  42.  
  43. progress.CurrentOperationText = "Step " + i.ToString();
  44. if (!Response.IsClientConnected)
  45. {
  46. //Cancel button was clicked or the browser was closed,so stop processing
  47. break;
  48. }
  49.  
  50. progress.TimeEstimated = (toVal - i) * 100;
  51. //Stall the current thread for 0.1 seconds
  52. System.Threading.Thread.Sleep(100);
  53. EndServ = i + 1;
  54. if (i == fromVal)
  55. {
  56. //--->STEP1
  57. //Load intial data
  58. int intial = Balance.PrepareIntialData(calcYear);
  59. //--->STEP2
  60. res = Balance.CalcEndServed(calcYear,EndServ - 1,6,30);
  61.  
  62. }
  63. //--->STEP3
  64. int newEmps = Balance.PrepareNewEmployees(calcYear,i);
  65.  
  66. for (int j = 0; j < 2; j++)
  67. {
  68. if (j == 0)
  69. {
  70. finMonth = 7;
  71. finYear = i;
  72.  
  73. }
  74. else
  75. {
  76. finMonth = 1;
  77. finYear = i + 1;
  78. }
  79. //--->STEP4
  80. int promotion1 = Balance.PreparePromotionFirst(finYear,finMonth,calcYear);
  81. //--->STEP5
  82. int promotion2 = Balance.PreparePromotionSecond(finYear,calcYear);
  83. //--->STEP6
  84. int appointment1 = Balance.PrepareAppointmentFirst(finYear,calcYear);
  85. //--->STEP7
  86. int appointment2 = Balance.PrepareAppointmentSecond(finYear,calcYear);
  87.  
  88. //--->STEP8
  89. int bonus = Balance.PrepareBonus(finMonth,finYear,calcYear);
  90.  
  91. //--->STEP9
  92. int salary = Balance.PrepareSalary(finYear,calcYear);
  93. (((CheckBox)gv_balance.Rows[index].Cells[3].FindControl("chk_redirect")).Checked == true)
  94. {
  95. //--->STEP9
  96. int acco = Balance.PrepareFinanceAccount(finYear,calcYear);
  97. }
  98.  
  99. }
  100.  
  101.  
  102. //--->STEP10
  103. res = Balance.CalcEndServed(calcYear,EndServ,30);
  104. Balance.CalcStudy(calcYear);
  105. UpdateProgressContext();
  106. if (res < 0)
  107. {
  108.  
  109. success_lb.Visible = false;
  110. error_lb.Visible = true;
  111. error_lb.Text = "ERROR";
  112.  
  113. }
  114. else
  115. {
  116. done = true;
  117. success_lb.Visible = true;
  118. error_lb.Visible = false;
  119. success_lb.Text = "Success";
  120. }
  121.  
  122.  
  123. }
  124. }

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

解决方法

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

服务器部分

我们首先映射SignalR.

  1. public class Startup
  2. {
  3. public void Configuration(IAppBuilder app)
  4. {
  5. // Any connection or hub wire up and configuration should go here
  6. app.MapSignalR();
  7. }
  8. }

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

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

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

  1. public class MyHub : Hub
  2. {
  3. public void Start(string arg)
  4. {
  5. Task.Run(() =>
  6. {
  7. AVeryLongTask();
  8. });
  9. }
  10.  
  11. //simulate a long task
  12. void AVeryLongTask()
  13. {
  14. for (int i = 0; i < 10000; i++)
  15. {
  16. Thread.Sleep(100);
  17. Clients.Caller.ReportProgress("AVeryLongTask",i * 100 / 10000);
  18. }
  19. }
  20. }

客户部分

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

  1. <!--Script references. -->
  2. <!--Reference the jQuery library. -->
  3. <script src="Scripts/jquery-1.6.4.min.js"></script>
  4. <!--Reference the SignalR library. -->
  5. <script src="/Scripts/jquery.signalR-2.0.0.js"></script>
  6. <!--Reference the autogenerated SignalR hub script. -->
  7. <script src="/signalr/hubs"></script>

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

  1. $(function() {
  2. // Declare a proxy to reference the hub.
  3. var hub = $.connection.myHub;
  4. // Create a function that the hub can call to report progress.
  5. hub.client.reportProgress = function(functionName,progress) {
  6. $('#progression').append('<li><strong>' + progress + '</strong>:&nbsp;&nbsp;' + functionName + '</li>');
  7. };
  8. // Start the connection.
  9. $.connection.hub.start().done(function() {
  10. $('#startlongprocess').click(function() {
  11. //start the long process
  12. hub.server.start("arg");
  13. alert("started");
  14. });
  15. });
  16. });

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

  1. <div class="container">
  2. <input type="button" id="startlongprocess" value="Send" />
  3. <ul id="progression"></ul>
  4. </div>

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

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

猜你在找的C#相关文章