Ano*_*ser 3 c# datetime date-arithmetic winforms
我有一个后台工作人员报告计算的进度.它返回它在ProgressChangedEventArgs中处理的当前行号.我可以从那里确定做的百分比
(int) (100 * (double)e.ProgressPercentage / csTextLines)
Run Code Online (Sandbox Code Playgroud)
其中csTextLines是我正在处理的行数.我遇到的麻烦是我想要得到总估计时间和剩余时间.我看到后台工作程序中没有任何内容可以帮助我,所以我想它必须使用DateTime计算完成.这是我到目前为止尝试获得TotalEstTime和TimeLeft的方法
哪里
哪里
所以,如果我们把它变成代码,我到目前为止
// This event handler updates the progress.
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
TimeSpan difInCalls = DateTime.Now.Subtract(m_LastProgressCall);
m_LastProgressCall = DateTime.Now;
m_totalDif = m_totalDif.Add(difInCalls);
TimeSpan avgDif = new TimeSpan(0, 0, 0, 0, ((int)m_totalDif.TotalMilliseconds / m_csNumLines));
double totalTime = m_csNumLines * avgDif.TotalSeconds;
double timeLeft = totalTime - (DateTime.Now.Subtract(m_Form2Start).TotalSeconds);
Console.WriteLine("TotalEstTime: " + totalTime + " TimeLeft: " + timeLeft);
// Update the progress label
resultLabel.Text = "Line " + e.ProgressPercentage.ToString() + " of " + m_csNumLines + " at " + (int)(100 * (double)e.ProgressPercentage / m_csNumLines) + "% loaded";
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我的TotalTime随着处理的行数而显着增长.在一个1437线的字符串中,它首先说我有1.4s,最后它估计还有18.6s.实际上需要16.54才能完成.
我的第二个问题是剩下的时间,并没有真正改变.它在大约1.1和2.5之间波动.
有什么可归因于?可能是什么问题呢?
以下是完整代码:http://pastebin.com/x1CCceY7
问题是你正在处理最后一个进度时间,这可能会随着你的进展而波动.如果你从开始时间和当前时间开始工作,并使用百分比完成,那么当你移动时,你会发现时间"自我调整".
例如,如果存储DateTime.Now到变量中(例如m_operationStart,使用您的命名),您可以编写:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (e.ProgressPercentage != 0)
{
double percentageComplete = (double)e.ProgressPercentage / m_csNumLines;
TimeSpan timeSinceStart = DateTime.Now.Subtract(m_operationStart);
TimeSpan totalTime = TimeSpan.FromMilliseconds(timeSinceStart.TotalMilliseconds / percentageComplete);
TimeSpan timeLeft = totalTime - timeSinceStart;
Console.WriteLine("TotalEstTime: " + totalTime + " TimeLeft: " + timeLeft);
// Update the progress label
resultLabel.Text = "Line " + e.ProgressPercentage.ToString() + " of " + m_csNumLines + " at " + (int)(100.0 * percentageComplete) + "% loaded";
}
else
resultLabel.Text = "Line " + e.ProgressPercentage.ToString() + " of " + m_csNumLines;
}
Run Code Online (Sandbox Code Playgroud)