Click here to Skip to main content
16,004,727 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I am writing an application in C# and it copies a few files from one folder to another and I need a way to calculate the time remaining to copy it over and then display it in a progress bar. Would this work?:

Time how long it takes to copy 1 byte of the file and then {multiply that time by the number of bytes left to copy of the file. Add that total time to DateTime.Now to get the time in the future when it will theoretically be copied and convert it to a percentage and display it in the progress bar.} (repeating the section in curly braces for the number of bytes left to copy each time the copied byte count increases)

But this may not be good for small files which are what most of the files I am copying are and it may be overtaken by the actual copying of the file. Is this a good way to calculate the time left to copy a file and if not what else could I do? Thank you
Posted

The way I've done it in the past is to just do it by file. You can take the number of files to be copied and each time you complete a file recalculate how long it took and how long it will take for the remaining files.

If you want to do this by size, you would have to average out more than 1 byte of copying, then add some overhead to the time. For example you can measure the time it takes to copy 1Kb, then multiply that by the total number of Kb's to be copied, and add maybe 10% more time for file system overhead. You will want to recalculate the time required after every X kb's copied (for example, recalculate time after each 10kb).

There isn't a real accurate way to do this, even MS can't get it right (by anybody who's watched the time copying files in Explorer). You have to recalculate often.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Dec-13 11:39am    
Agree, a 5. I also answered at the same time...
—SA
Files are written to disk in blocks (say 4K at a time), with automatic buffering. So writing a single byte tells you nothing.

I'd suggest that you benchmark full write of files of increasing size and do not use the progress bar on the files that take, say, shorter than 2/10th of a second. And for the others, update the progress bar in steps of that size.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 9-Dec-13 11:39am    
That's correct, a 5. However, I would advise to use just time and total size, copied vs total. As I tried to explain in my answer, in general case the time estimate will never be quite accurate...
—SA
Unfortunately, there is absolutely no way to predict the time of such operation with any reasonable accuracy. You can easily see this inaccuracy when you use some download manager of any Web browser showing percentage and expected time of completion. Often, expected time is oscillating back and forth back and force, changing seconds to hours and back. There are many reasons for that: traffic and operational environment changes, the chunks of data being downloaded change its typical size (say, long files are changed by a set of many very small ones), and so on.

When you copy files on the same machine, the situation is much more stable, but similar factors (except traffic) are still in play.

All you can do is to estimate remaining time by getting the time of already copies part and the percentage of already copied data. Not be surprised to see that the estimate is very inaccurate. I might feel some relief if you see how inaccurate is the estimate when you copy files with Explorer or any other file manager. There are multiple jokes about Microsoft in this regard; did you hear them?

For timing, it's always better to use System.Diagnostics.Stopwatch instead:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Comments
Ron Beyer 9-Dec-13 11:40am    
Those jokes go back to Win 95! :) +5
Sergey Alexandrovich Kryukov 9-Dec-13 11:50am    
Windows versions come and go, but the some behaviors look the same... :-)
Thank you, Ron.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900