by DiamondDrake
24. October 2010 22:00
Sometimes when a proccess is long enough users of your applications might like to know about how long your proccess has left. This isn't always the easisest task, but in the event that you have a set number of itterations, or a consistently changing percentage you can calculate the time remaining very easily. The idea is that you measure the time from when you started til the current itteration and devide it by the current itteration giving you the average time of each itteration. then multiply that by the itterations left to get the estimated time remaining. so the formula at its simpilest is
Time Remaining = (currentTime / currentItteration) * itterationsRemaining
This formula can also work with a percentage being Time Remaining = (currentTime / currentPercent) * precentRemaining
Here is an example of using this formula in C#
void looping_Proccess(int count)
{
DateTime startTime = DateTime.Now;
for (int i = 0; i < count; i++)
{
doLongThing(i);
TimeSpan currenttime = DateTime.Now - start;
if(i > 0) //Prevent devide by zero
{
TimeSpan TimeRemaining = TimeSpan.FromSeconds((currenttime.TotalSeconds / i) * (count - i));
reportTimeRemaining(TimeRemaining);
}
}
}