我有一个这么长的过程可能需要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, finMonth, calcYear);
//--->STEP6
int appointment1 = Balance.PrepareAppointmentFirst(finYear, finMonth, calcYear);
//--->STEP7
int appointment2 = Balance.PrepareAppointmentSecond(finYear, finMonth, calcYear);
//--->STEP8
int bonus = Balance.PrepareBonus(finMonth, finYear, 0, calcYear);
//--->STEP9
int salary = Balance.PrepareSalary(finYear, finMonth, calcYear);
(((CheckBox)gv_balance.Rows[index].Cells[3].FindControl("chk_redirect")).Checked == true)
{
//--->STEP9
int acco = Balance.PrepareFinanceAccount(finYear, finMonth, calcYear);
}
}
//--->STEP10
res = Balance.CalcEndServed(calcYear, EndServ, 6, 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";
}
}
}
字符串
我想显示当前步骤,例如:(Promotion 1 ) in ---> 1-2018
和整个过程的百分比旁边的估计时间。
3条答案
按热度按时间j8ag8udp1#
要使用signalR报告一个非常长的任务的进度,你可以这样做(这只是一个例子来展示它是如何工作的):
服务器部分
我们从MapSignalR开始。
字符串
我们创建一个hub类(不要忘记安装signalr包):
(If你想向所有连接的用户或特定的用户组报告进度,请在这里查看:http://www.asp.net/signalr/overview/guide-to-the-api/working-with-groups)
在给定的示例中,它只向
Start
函数的调用者报告进度。型
客户端部件
在HTML中,你必须添加这些引用:
型
现在是Js部分,用于从中心获取进度:
型
进度和开始按钮的html容器:
型
如果你需要更多的解释,请不要犹豫。
(我的示例基于signalr团队的http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr)
vojdkbi02#
您可以使用网络套接字将进度更新推送到客户端。SignalR是一个. net库,它 Package 了网络套接字,并在网络套接字不可用的地方回退。网上有很多例子展示了如何使用SignalR实现进度报告,因此无需重复。请看这里:
https://github.com/dragouf/SignalR.Progress
或在此处:
https://www.safaribooksonline.com/blog/2014/02/06/server-side-signalr/的
例如,可以使用其它类型的装置。
xytpbqjk3#
这里有一个简化的问题,我认为可以解决你的问题。如果你在一个任务中运行你的长操作,你可以用一个状态对象更新应用程序。如果你的应用程序是WPF,并且你绑定了状态,它会自动更新。在WinForms中,你可以绑定或只是实现事件处理程序。
字符串