5

I am processing some xml files in for loop and according to the number of files has been processed I want to show the progress bar. Suppose there are 100 files in directory and files are processing one by one in loop and I want to update the progress bar according to the current count of the for loop.
Please suggest..

nitendra jain
  • 433
  • 4
  • 8
  • 16
  • update to the iterator in the loop. (if the loop is `for (int i = 0; i < 100; i++)` the iterator is i). – elyashiv Sep 27 '12 at 09:11

5 Answers5

3

Process the 100 files using a Background Worker, call ReportProgress every iteration, hook on to the Process changed event of the backgroundworker and update a progressbar accordingly.

You can check out this tutorial for details.

Ben
  • 1,133
  • 1
  • 15
  • 30
zeebonk
  • 4,864
  • 4
  • 21
  • 31
3

Take a look at BackgroundWorker class, particularly, at ProgressChanged event.

Dennis
  • 37,026
  • 10
  • 82
  • 150
2

You should use BackgroundWorker combined with a ProgressBar control. Here is a simple example.

Yiğit Yener
  • 5,796
  • 1
  • 23
  • 26
0
for(int i=1;i<linecount;i++)
{
 progressBar1.Value = i * progressBar1.Maximum / linecount;  //show process bar counts
 LabelTotal.Text = i.ToString() + " of " + linecount; //show number of count in lable
 int presentage = (i * 100) / linecount;
 LabelPresentage.Text = presentage.ToString() + " %"; //show precentage in lable
 Application.DoEvents(); keep form active in every loop
}
  • 1
    Please don't add the [same answer to multiple questions](http://meta.stackexchange.com/questions/104227/is-it-acceptable-to-ad??d-a-duplicate-answer-to-several-questions). Answer the best one and flag the rest as duplicates, once you earn enough reputation. If it is not a duplicate, tailor the post to the question – Manfred Radlwimmer Mar 24 '17 at 09:04
  • @ManfredRadlwimmer Sorry i am new here.thanks for the advice and i will follow in future. – Gayan Chinthaka Dharmarathna Mar 24 '17 at 09:09
0

Set Minimum and Maximum and then use the Step property with the PerformStep method to increment the value of the ProgressBar.

 progressBar1.Step = 1;
 int part=someList.Count / 100;
 ....
 .... 
 //Inside the loop 

  if (loop_counter % part == 0)
  {
       progressBar1.PerformStep();
  }

See

TiyebM
  • 2,684
  • 3
  • 40
  • 66