0

I have an app that reads headers of files, I'd like to implement a progress bar but I'm having issues trying to get it to work based off this Microsoft example.

The example works off files copied.

I'd like the bar to progress with each file, with my code below all it does is go to max once all the files have been read. Folders I open to read file headers can have 1 file or 1000's.

I'm lost with this bit, plus I may have the code in the wrong spot...

if (CopyFile(files[x-1]) == true)

I don't think my Threading is working well either, so some advice on it would also be great.

private void btnOpenFile_Click(object sender, EventArgs e)
    {
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            ThreadStart t = delegate {};
            listBoxResults.Items.Clear();
            string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
            txtFilesFound.Text = files.Length.ToString();
            {
                string path = folderBrowserDialog1.SelectedPath;
                foreach (string filepath in Directory.GetFiles(path, comboFileType.Text, SearchOption.TopDirectoryOnly))
                {

                    foreach (string item in GetHeaderInformation(filepath))
                    {

                        this.Invoke(new Action(() => txtUpdate(item)));
                        // Display the ProgressBar control.
                        pBar1.Visible = true;
                        // Set Minimum to 1 to represent the first file being copied.
                        pBar1.Minimum = 1;
                        // Set Maximum to the total number of files to read.
                        pBar1.Maximum = files.Length;
                        // Set the initial value of the ProgressBar.
                        pBar1.Value = 1;
                        // Set the Step property to a value of 1 to represent each file being read.
                        pBar1.Step = 1;
                    }
                    this.Invoke(new Action(() => txtUpdate(filepath + Environment.NewLine)));
                    this.Invoke(new Action(() => txtUpdate("***************************************************************************")));
                    for (int x = 1; x <= files.Length; x++)
                       //if(CopyFile(files[x-1]) == true)
                            //{
                            // Perform the increment on the ProgressBar.
                                pBar1.PerformStep();
                            //}
                }
            };
            new Thread(t).Start();
        }
    }

Any help would be appreciated.

Perhaps this example might be more beneficial.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • 3
    That code does not look thread safe to me. Have a look at the [BackgroundWorker](http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx) if using winforms. [Here's a simple example](http://stackoverflow.com/a/25446690/1895201) I've posted elsewhere. – DGibbs Sep 22 '14 at 15:57

1 Answers1

0

Progress bars run on the UI thread. After you update the progress bar, the UI thread has to return from your code and continue processing windows messages in order to allow the window repainting to occur.

If your code runs in a busy loop, none of the progress updates can be processed until your loop has completed and returned control to the main application loop. After starting your thread you therefore need to ensure your event handler returns control rather than sitting and waiting or doing other work.

You need to either work using periodic updates on a timer, or (better) run your processing in a background thread and just pass progress updates back to the UI thread periodically. however, if you just prod the ui control from another thread you will get a cross thread person and undefined results.

Most progress bar examples should show you how to do the latter using a BackgroundWorker or similar.

Jason Williams
  • 56,972
  • 11
  • 108
  • 137