I have a windows desktop application that I am writing using Windows Forms in visual basic. In this app I would like to display a simple progress bar, but I have come across a strange issue. The below is an example of a simple for loop which updates the progress bar:
pBar.Visible = true;
// pBar.Minimum and pBar.Step are both set to 1 using the properties pane in the design page
pBar.Maximum = 100;
for (int j = 0; j < 100; j++) {
double pow = Math.Pow(j, j); //Calculation
pBar.PerformStep();
}
The above works fine, however, I would like to be able to reset the progress bar to use it again later. I assumed I could simply set pBar.Value = 1
after the for loop, but strangely this breaks the progress bar. Instead of incrementing steadily like it did previously, the bar does not fill at all. If I set pBar.Value = 50
, the bar begins at the halfway point and does not move. It is behaving like pBar.Value
is executing before the for loop, or that pBar.Value
is getting set during each loop iteration (I've made sure that I did not mistakenly put the pBar.Value
statement inside the for loop).
The only reason I could think this is happening, is that C# is running my for loop asynchronously and the value is getting set immediately, then for some reason it isn't getting updated. I've tried looking around for answers to this, but so far all I have found are ways to create background workers (I'm not interested in that) and that my method of resetting the progress bar should be working...
Why is this happening?