Max
Max

Reputation: 13

Increment ProgressBar within another class

it's my first question I'm asking here, so please be gentle with me ;)

So I've actually got two WinForms in my C# application I'm writing at the moment (I'm quite new to C#).

This window has a button, which saves photos from an usb device you selected before in a list box to another folder. After clicking on this button my main thread is of course busy with copying, so I decided to create another WinForm which contains my ProgressBar.

Foreach completed copy, I want to increment my ProgressBar accordingly.

So I count the number of copies I have to do and give it the progressbar as maximum. But my problem at the moment is, that I really don't know how to increment the ProgressBar without getting a Thread Unsafe Exception.

Here's my ProgressWindow code:

public partial class ProgressWindow : Form
{
    BackgroundWorker updateProgressBarThread = new BackgroundWorker();

    private Boolean _isThreadRunning = false;
    public Boolean IsThreadRunning
    {
        get { return _isThreadRunning; }
        set { _isThreadRunning = value; }
    }

    private int _progressbarLength;
    public int ProgressbarLength
    {
        get { return _progressbarLength; }
        set { _progressbarLength = value; }
    }

    private int progress = 1;

    public ProgressWindow()
    {
        Show();
        InitializeComponent();
    }

    private void StartUpdateThread(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        // Reports progress to the ProgressChangedEvent function. (Thread Safe)

    }

    private void FinishProgressThread(object sender, RunWorkerCompletedEventArgs e)
    {
        if (!_isThreadRunning)
        {
            MessageBox.Show("Erfolgreich kopiert");
            Close();
        }

    }

    private void ProgressChangedEvent(object sender, ProgressChangedEventArgs e)
    {
        this.copyProgressbar.Value = e.ProgressPercentage;
        this.progressStatus.Text = e.ProgressPercentage.ToString();
    }

    public void CallUpdateThread()
    {
        updateProgressBarThread.WorkerReportsProgress = true;

        updateProgressBarThread.DoWork += new DoWorkEventHandler(StartUpdateThread);
        updateProgressBarThread.ProgressChanged += new ProgressChangedEventHandler(ProgressChangedEvent);
        updateProgressBarThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(FinishProgressThread);
        updateProgressBarThread.RunWorkerAsync();
    }

}

I want to increment my ProgressBar with 1 after each succesful copy. How do I do this from my main thread?

This is the function which actually handles the copy process

private void SaveFile(System.IO.DirectoryInfo root)
{
    try
    {
        IEnumerable<DirectoryInfo> directoriesNames = root.EnumerateDirectories();

        // New instance of thread ProgressWindow.
        ProgressWindow progress = new ProgressWindow();
        progress.CallUpdateThread();

        foreach (DirectoryInfo element in directoriesNames)
        {
            // Query all subdirectories and count everything with the in the configuration made settings.
            if (!element.Attributes.ToString().Contains("System"))
            {
                // Now we insert the configuration we applied.
                String fileExtension = null;

                if (Properties.Settings.Default._configPhoto)
                {
                    fileExtension = "*.jpg";
                }

                if (Properties.Settings.Default._configWordDocument)
                {
                    fileExtension = "*.odt";
                }

                FileInfo[] jpgList = element.GetFiles(fileExtension, SearchOption.AllDirectories);

                // set the size of the progress bar
                progress.ProgressbarLength = jpgList.Count();

                // Now we go through all our results and save them to our backup folder.
                foreach (FileInfo tmp in jpgList)
                {
                    string sourceFilePath = tmp.FullName;
                    string destFilePath = PATHTOBACKUP + "\\" + tmp.Name;
                    progress.IsThreadRunning = true;

                    try
                    {                                
                        System.IO.File.Copy(sourceFilePath, destFilePath, true);
                    }
                    catch (IOException ioe)
                    {
                        MessageBox.Show(ioe.Message);
                    }
                }
            }
        }
        // progress.IsThreadRunning = false;
    }
    catch (UnauthorizedAccessException e)
    {
        MessageBox.Show(e.Message);
    }
}

It's pretty obvious that I have to do this after this function

System.IO.File.Copy(sourceFilePath, destFilePath, true);

But how do I report this to my ProgressWindow?

I really hope I explained it well enough, not sure if I'm missing something important.

Thanks in advance guys

Upvotes: 1

Views: 1283

Answers (1)

Loathing
Loathing

Reputation: 5266

Here is a compact example of the key components:

  • Clicking button starts new thread worker
  • Progress is done by file lengths, not by number of files
  • BeginInvoke used to update the progress bar (avoid cross Thread exception)

        ProgressBar pb = new ProgressBar() { Minimum = 0, Maximum = 100 };
        Button btn = new Button();
        btn.Click += delegate {
            Thread t = new Thread(() => {
                DirectoryInfo dir = new DirectoryInfo("C:\\temp\\");
                var files = dir.GetFiles("*.txt");
                long totalLength = files.Sum(f => f.Length);
                long length = 0;
                foreach (var f in files) {
                    length += f.Length;
                    int percent = (int) Math.Round(100.0 * length / totalLength);
                    pb.BeginInvoke((Action) delegate {
                        pb.Value = percent;
                    });
    
                    File.Copy(f.FullName, "...");
                }
            });
            t.IsBackground = true;
            t.Start();
        };
    

Upvotes: 1

Related Questions