1

I have a program that takes loops through files in a folder, creates a new folder elsewhere with that file name, then puts a common file in that folder. rinse repeat until we are done with whats in that folder (listed in a listbox).

I decided I want to be fancy and have a progress bar. I conceptually understand that I am increment the count by one (to the listbox.items.count) for each iteration.. which I have coded and works well.

I also understand that the DoWork event is what counts for the progress bar, how do I get the two to mesh? Do I pass a counter to the DoWork for each iteration?

I am just missing the bridge from my main code loop to the DoEvents counter.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Darw1n34
  • 312
  • 7
  • 24
  • `DoWork` has little to do with the progressbar, it is a BackGroundWorker method. If that is what you are using, use the `ReportProgress` method to allow the UI to update the progess bar. `DoEvents` is not involved – Ňɏssa Pøngjǣrdenlarp Jul 02 '15 at 18:35
  • @marc_s OK, thanks for the -2, hope that makes you feel good. Cheers. – Darw1n34 Jul 02 '15 at 18:51
  • @Plutonix Thanks, I think I get it, I will play around with it. – Darw1n34 Jul 02 '15 at 18:52
  • I **did NOT** downvote you - I could now, just to prove what I'm saying .... (but I won't - your question's isn't bad or stupid or anything) - I only fixed a few typos and cleaned up your post a bit. Thanks for nothing, I guess.... – marc_s Jul 02 '15 at 19:48

1 Answers1

1

Use a backgroundworker as shown:

 BackgroundWorker worker = new BackgroundWorker();
 worker.WorkerReportsProgress = true;
 worker.DoWork += new DoWorkEventHandler(update_DoWork);
 worker.ProgressChanged += new ProgressChangedEventHandler(update_ProgressChanged);
 worker.RunWorkerAsync();

You need a ProgressChangedEventHandler. Then you will need to use ReportProgress when you want to update the progress bar (this will be in the DoWork method). This method will call the ProgressChangedEventHandler.

int percentProgress = 10;
worker.ReportProgress(percentProgress)

An example of a ProgressChangedEventHandler:

 private void update_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
           progressBar.Value = (e.ProgressPercentage);
        }
Ryan Ward
  • 61
  • 5