Update progress bar from for loop

前端 未结 5 719
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 23:26

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

相关标签:
5条回答
  • 2021-01-18 23:44

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

    0 讨论(0)
  • 2021-01-18 23:47
    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
    }
    
    0 讨论(0)
  • 2021-01-18 23:59

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

    0 讨论(0)
  • 2021-01-19 00:01

    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.

    0 讨论(0)
  • 2021-01-19 00:05

    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

    0 讨论(0)
提交回复
热议问题