BackgroundWorker & Progressbar Issues c# Visual St

2019-08-04 12:35发布

问题:

Im trying to get my head around backgroundworker and the progressbar, so far i have got it to work but not exactly how i want it to work. Basically i am sorting/renaming folders and copying them to a different location, this works and the code is self explanatory, the output folders generated are as expected. However for each folder i intend to search through i have to right click it to get the number of files and then in the code i have to set the progressBar1.Maximum to that value in order for it to show the coreect progression of the progress bar. How is it possible to get this to set the number of files automatically since it goes through each folder anyway? Some folders have thousands of files and others have millions. beyond this i want to add a label so that it displays the name of the file it is processing along with the progressbar updates.

namespace Data_Sorter
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnSelect_Click(object sender, EventArgs e)
    {
        folderBrowserDialog1.ShowDialog();
        tbFilePath.Text = folderBrowserDialog1.SelectedPath.ToString();
    }

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

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int totalFiles = 0;
        foreach (var file in Directory.GetFiles(tbFilePath.Text, "*.txt", SearchOption.AllDirectories))
        {
            backgroundWorker1.ReportProgress(totalFiles);

            string fullFilename = file.ToString();

            string[] pathParts = fullFilename.Split('\\');
            string date = pathParts[6];

            string fileName = pathParts[7];

            string[] partName = fileName.Split('_');

            string point = partName[3];

            Directory.CreateDirectory("Data Sorted Logs\\" + point + "\\" + date + "\\");

            if (Directory.Exists(("Data Sorted Logs\\" + point + "\\" + date + "\\")))
                {
                    string destPath = (point + "\\" + date + "\\");
                    File.Copy(fullFilename, "C:\\Documents and Settings\\PC\\Desktop\\Sorter\\Data Sorter\\bin\\Debug\\Data Sorted Logs\\" + destPath + fileName);                    }
            else
                {
                    MessageBox.Show("destination folder not found " + date + point);
                }

            totalFiles++;
        }
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Done");
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Maximum = 6777; // set this value at the maximum number of files you want to sort //
        progressBar1.Value = e.ProgressPercentage;
    }
}

回答1:

You can find out the file number simply reading the GetFiles length.

You can pass the relative percentage using the expression: (i * 100) / totalFiles, in this way it's not necessary to set the Maximum value for the progress.

You can also report the filename to the progressbar passing it as the UserState in the progressChanged event.

Try the code below:

   namespace Data_Sorter
    {
    public partial class Form1 : Form
    {


public Form1()
    {
        InitializeComponent();
    }

    private void btnSelect_Click(object sender, EventArgs e)
    {
        folderBrowserDialog1.ShowDialog();
        tbFilePath.Text = folderBrowserDialog1.SelectedPath.ToString();
    }

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

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int totalFiles = 0;
        string[] files = Directory.GetFiles(tbFilePath.Text, "*.txt", SearchOption.AllDirectories);
        totalFiles = files.Length;
        int i = 0;
        foreach (var file in files)
        {

            backgroundWorker1.ReportProgress((i * 100) / totalFiles, file);
            i++
            string fullFilename = file.ToString();

            string[] pathParts = fullFilename.Split('\\');
            string date = pathParts[6];

            string fileName = pathParts[7];

            string[] partName = fileName.Split('_');

            string point = partName[3];

            Directory.CreateDirectory("Data Sorted Logs\\" + point + "\\" + date + "\\");

            if (Directory.Exists(("Data Sorted Logs\\" + point + "\\" + date + "\\")))
                {
                    string destPath = (point + "\\" + date + "\\");
                    File.Copy(fullFilename, "C:\\Documents and Settings\\PC\\Desktop\\Sorter\\Data Sorter\\bin\\Debug\\Data Sorted Logs\\" + destPath + fileName);                    }
            else
                {
                    MessageBox.Show("destination folder not found " + date + point);
                }

            totalFiles++;
        }
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Done");
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        progressBar1.Text = e.UserState.ToString();//or yourNewLabel.Text = e.UserState.ToString();
    }
}


回答2:

Move the call to GetFiles up so you can get the length of the array it returns:

string[] files = Directory.GetFiles(tbFilePath.Text, "*.txt",
                                    SearchOption.AllDirectories));

// Note - you won't be able to set this UI property from DoWork 
// because of cross-thread issues:
// progressbar1.Maximum = files.Length; 

int fileCount = files.Length;   

foreach (var file in files ...