简体   繁体   中英

C# Pogressbar & Backgroundworker

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...
        }
}

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);
        }
    }
}
  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?

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++;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM