Normally, a progress bar is used to indicate how far a task has progressed, and thus to give the user an idea how long it will take to complete.
If you replace the
if
with
while
:
while (progressBar1.Value < progressBar1.Maximum)
progressBar1.Value = progressBar1.Value + 1;
then it is effectively executed as a single statement:
progressBar1.Value = progressBar1.Maximum
What you would normally do it to start the progress bar at the beginning of a task, and increase the value each time you had done some significant part of the work. For example, if you were processing ten files, the the maximum value might be ten, and you would increase the Value property after each file was processed.
However, if you are using a progress bar, then normally the processing is done in a separate thread, to get the User Interface nice and fluid - the only problem being that you need to Invoke controls in the processing thread as it cannot access UI controls.
If you use a System.ComponentModel.BackgroundWorker, there's no need for you to do the invoking.
The BackgroundWorker offers status reporting across thread bounderies on its own.
1. Set the BackgroundWorker.WorkerReportsProgress property to true.
2. Within the worker routine call ((BackgroundWorker)sender).ReportProgress().
3. Do the UI changes (including progress bar) within BackgroundWorker.ProgressChanged event handler.