My 2 cents:
Record the start time when the process starts. I do this by setting a property on the window:
StartTime = ticks
I also suggest keeping an array of calculated times. If something causes your app to slow down, this will auto compensate:
TotalSecondsCalcs() as Integer
You’ll need a method for only averaging the most recent calculations:
Private Function Avg(numbers() as Integer, count as integer) As Integer
dim tot as integer
dim c as integer = min(numbers.ubound,count - 1)
for i as integer = 0 to c
tot = tot + numbers(i)
next i
return tot/(c+1)
End Function
Then call this method to calculate the number of seconds remaining.
[code]Private Function UpdateTimeRemaining(percentageComplete as Integer) As Integer
//What time is it now
dim currentTime as integer = ticks / 60
//How many seconds have elapsed
dim deltaSeconds as integer = currentTime - StartTime
//Based on the percentage complete, how many seconds will the whole process take
dim totalSeconds as integer = deltaSeconds / (percentageComplete/100)
//Add this value to the array
TotalSecondsCalcs.Insert 0,totalSeconds
//Get an average of the last 10 totals
dim averageTotal as integer = avg(totalSecondscalcs,10)
//Return the number of seconds we THINK it’s going to be
dim secondsRemaining as integer = averageTotal - deltaSeconds
return max(secondsRemaining,0)
End Function
[/code]
You may also want to make it say “Calculating…” while the array gets populated. Otherwise the original estimates will wildly vary.