I have a background worker reporting the progress of the computation. It returns the current line number it is processing in the ProgressChangedEventArgs. I can from there determine the percentage done by doing
(int) (100 * (double)e.ProgressPercentage / csTextLines)
where csTextLines is the number of lines I am processing. The trouble I am running into is that I want to get a total estimated time and total time left also. There is nothing in background worker that I saw that would help me so I guess it has to be done with DateTime calculations. Here is what I have done so far to try and get TotalEstTime and TimeLeft
- TimeLeft = TotalEstTime – TimeSoFar;
- TotalEstTime = AvgTimeForEachLine * #OfLines;
Where
- TimeSoFar = DateTime.Now – m_startTime;
- AvgTimeForEachLine = (TimeFromLastLine + TotalTimeForAllLines) / #OfLines;
Where
- TimeFromLastLine = DateTime.Now – LastRecordedTime;
- TotalTimeForAllLines += TimeFromLastLine;
So if we turn this into code I have so far
// 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";
}
The problems that I am having are that my TotalTime grows significantly with how many lines are processed. In a 1437 line string it starts out at saying I have 1.4s and at the end it estimates 18.6s left. While actually taking 16.54s to complete.
My second problem is that the time left, does not really change. It fluctuates between about 1.1 and 2.5.
What could there be attributed to? What could be the problem?
Here is the full code: http://pastebin.com/x1CCceY7
The problem is that you’re working off the last progress time, which will likely fluctuate as you go. If you work from the starting time and the current time, and use the percentage complete, then you’ll find that the time “self adjusts” as you move along.
For example, if you store
DateTime.Nowinto a variable (such asm_operationStart, using your naming), you could write: