0

When clicking on a button my program will do the following: The program copies files to a tempfolder and creates a zip file out of the tempfolder. The paths to the files which get copied are stored in an array. Just to make things clear:

// "files" has stored the paths

private void button2_Click(object sender, EventArgs e)
{
   foreach (var file in files)
   {
       File.Copy(file, tempPath + @"\" + System.IO.Path.GetFileName(file));
   }
}

I want to include a progressbar into my form which gives feedback about the made progress. For every copied file the progressbar should move.

I am struggleing about how and where to report the progress.

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
        int steps = files.Length;

        for (int i = 0; i < steps; i++)
        {
            // Do something...
        }
}
loyd
  • 147
  • 1
  • 8

3 Answers3

2

You need to register a handler on the ProgressChanged event, and call ReportProgress(percentage) in the DoWork method.

Example:

class Program
{
    private static BackgroundWorker _worker;

    static void Main(string[] args)
    {
        _worker = new BackgroundWorker();
        _worker.DoWork += Worker_DoWork;
        _worker.ProgressChanged += Worker_ProgressChanged;
        _worker.WorkerReportsProgress = true;
        _worker.RunWorkerAsync();
        Console.ReadLine();
    }

    private static void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        Console.WriteLine("Progress is {0}", e.ProgressPercentage);
    }

    private static void Worker_DoWork(object sender, DoWorkEventArgs e)
    {
        var worker = (BackgroundWorker)sender;
        for (int i = 0; i < 100; ++i)
        {
            worker.ReportProgress(i); // Reporting progress in percent
            Thread.Sleep(50);
        }
    }
}
Oystein
  • 1,232
  • 11
  • 26
1
  1. Set the Maximum property of the ProgressBar to your file count. This way you don't have to calculate percentages yourself.

  2. In the loop where you copy your files simply increment the Value property of the ProgressBar.

  3. It doesn't make sense to copy the files in the UI thread and update the ProgressBar in a background thread, if it really takes too long, it should be the other way around.

This answer might further help as well: How to update GUI with backgroundworker?

Markus Dresch
  • 5,290
  • 3
  • 20
  • 40
0

I figured it out myself: For everyone who has an array and wants to work with a progressbar and a backgroundworker (There might be better ways):

private void Button (object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
            var files = Directory.GetFiles(startPath, "*" + filetype, SearchOption.AllDirectories);
            int max = files.Length;
            int i = 0;

        foreach (var file in files)
        {
            File.Copy(file, tempPath + @"\" + System.IO.Path.GetFileName(file));
            backgroundWorker1.ReportProgress((i * 100) / max);
            i++;
        }
    }
loyd
  • 147
  • 1
  • 8
  • 1
    I would hardly call it "figured it out yourself" when you're given an answer containing information that you use in your final solution. But hey, you're welcome... – Oystein Aug 09 '18 at 12:12
  • @Oystein My main problem was setting up the .ReportProgress loop in connection with an array. I am still a beginner and sometimes even the easiest things seem complicated because I never knew better at this point. Still I want to thank you for your comment. – loyd Aug 09 '18 at 12:45
  • You said nothing about `ReportProgress` being a problem in your question. You only said you needed help with how and where to report progress in a `BackgroundWorker`, which I provided. – Oystein Aug 09 '18 at 13:04
  • @Oystein I provided code to make clear how my case looks like. I wanted to build a report for progress, yes. But nobody said that using the "for" loop to update is not possible in my case. I don't want any useless discussions so like I said: thank your for your help and time. – loyd Aug 09 '18 at 13:15