0

I'm currently using a loop in C# to generate files, but the program takes a few seconds to actually do this and I feel the program user would benefit from a progress bar that updates to tell them how far the loop is through so they can estimate when the loop is going to finish and all of their files are generated.

I was wondering if there was a way to calculate the time it's going to take a loop to complete or update a progress bar with the loop to show how much progress the loop has left.

Here's my loop.

String GeneratedLevelName;
int levelx = 0;
int levely = 0;

for (int i = 0; i < GmapLevelArea; i ++) {
  if (levelx >= (GmapWidth - 1)) {
    levelx = 0;
    levely ++;

    GeneratedLevelName = (GmapPrefix + "_" + levelx + "_" + levely + ".nw");
    File.Copy(ApplicationDirectory + TemplateFileName, GmapDirectory + GeneratedLevelName);
    GmapFile.AppendLine('"' + GeneratedLevelName + '"' + ",");
  } else {
    levelx ++;

    GeneratedLevelName = (GmapPrefix + "_" + levelx + "_" + levely + ".nw");
    File.Copy(ApplicationDirectory + TemplateFileName, GmapDirectory + GeneratedLevelName);
    GmapFile.Append('"' + GeneratedLevelName + '"' + ",");
  }
}

Any help is greatly appreciated.

svick
  • 236,525
  • 50
  • 385
  • 514
  • 1
    the timing you'd have to calculate manually. it's possible of course. you could also just show the progress made (like fire events every iteration you made). the latter could be enough if the time per iteration is more or less equal – bas Mar 16 '13 at 13:07
  • That's a good alternate suggestion actually. I'll look into calculating the time manually though. Thanks for your help. – Aaron Yarborough Mar 16 '13 at 13:10
  • If your files are big enough and you want to get a real progress bar for copying bytes, take a look at these questions: [Can I show file copy progress using FileInfo.CopyTo() in .NET?](http://stackoverflow.com/questions/187768/can-i-show-file-copy-progress-using-fileinfo-copyto-in-net) and [File Copy with Progress Bar](http://stackoverflow.com/questions/6044629/file-copy-with-progress-bar) – psubsee2003 Mar 16 '13 at 13:22
  • 1
    This link won't help: http://xkcd.com/612/ – Eric Lippert Mar 16 '13 at 14:51

2 Answers2

1

Since it would be difficult to time the File.Copy, I think you should just base your progress bar on total files worked on.

So if you have a progress bar: pb

pb.Minimum = 0;
pb.Maximum = GMapLevelArea;

then set the value of the progress bar to you i value

pb.Value = i;

Now when I usually use progress bars like this, I usually don't update ever iteration. I usually update after a few iterations.

So to do that I usually check for the iteration count:

if(i % 10 == 0) //only true when i is a multiple of 10
 pb.Value = i;

Then at the end of the loop, if it doesn't happen to be a multiple of your update, you can either set the progress bar to its maximum value or reset it to zero to let them no its done.

pb.Value = GMapLevelArea; or pb.Value = 0;
Tim Felty
  • 152
  • 12
0

It depends on how consistent your files are generated (do they all take a second, or take some longer than others?) and if you know how many you are going to generate. In the easiest case, you can estimate the time by taking the number of files to generate. For example if you create 50 files, you can increment the progress bar by two percent after a file is generated.

Lennart
  • 9,657
  • 16
  • 68
  • 84
  • These were my initial thoughts. I could use common sense and make a decent estimate, increasing the progress bar every time a file is generate. The only issue I can see with this though, is that different computers will find it easier/harder to generate files and will do it at different speeds. A slower computer may take up to a minute while a faster one could take a matter of seconds. – Aaron Yarborough Mar 16 '13 at 13:12
  • That still doesn't change the _progress_ of your iteration. If 10 files are generated, the percentage is the same on fast and slow computers. The only difference there is how fast the progress bar increases. The speed would only matter if you would want to estimate the time left for the operation, in that case you can take a look here: http://stackoverflow.com/questions/473355/calculate-time-remaining – Lennart Mar 16 '13 at 13:20