"One of our desktop applications has a progress bar in it," Bryce N. writes, "and as I was working more and more with the code, I noticed that the progress bar would progress to a seemingly random part in the bar, but never past the halfway mark. This would probably be ignored, if it weren't for the fact that I noticed that my breakpoints would only be hit when the bar reached the 'random' mark."
"While I was trying to discover why, I found this in the code:"
/* There is really NO eloquent way of calculating what the progress
* of a given method/task will be. One task may be downloading or copying a file while another
* one might be grabbing huge chunks of data for file creation. Since we want to see
* a progress indicator but can't determine this value, we'll simply play with it so it has the
* appearance of running; (i.e. we'll take it to 50%, execute the task, then come back and finish
* the progress upon completion.) This is a Microsoft STANDARD... I'm sure of it!
*/
Random R = new Random();
int _percentage = R.Next(Convert.ToInt32(.5 * _bar.Maximum));
string status = "";
for (int i = 0; i < _bar.Maximum; i++)
{
if (i == _percentage)
{
//execute the real code
}
// Always perform progress step
_bar.PerformStep();
}
"Well, there you have it, the code tells the progress bar to progress somewhere between 0 and 1/5 the length of the bar, then execute the real code. Brillant!"