BackgroundWorker的进度和问题C#的Visual Studio 2010(Backgr

2019-10-17 12:00发布

我试着让我周围的BackgroundWorker和进度头,到目前为止,我已完成了工作,但不正是我想要它是如何工作的。 基本上我排序/重命名文件夹,并将它们复制到不同的位置,这工作和代码是自解释的,所生成的输出文件夹是如预期。 然而,对于每个文件夹我打算通过搜索我必须右键单击它得到的文件数,然后在代码中,我必须设置progressBar1.Maximum该值,以便它显示进度条的coreect进展。 这怎么可能得到这个自动设置文件的数量,因为它经过的每个文件夹呢? 一些文件夹有成千上万的文件等都有数以百万计。 超出此我想添加一个标签,以便它显示它是随着进度的更新处理的文件的名称。

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

Answer 1:

你可以找到简单地读取的GetFiles长度文件编号。

你可以通过使用表达式的相对百分比: (i * 100) / totalFiles ,这样就没有必要设置进度的最大值。

您还可以将文件名报告把它当作在progressChanged事件UserState的进度。

试试下面的代码:

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


Answer 2:

向上移动调用的GetFiles这样你就可以得到它返回数组的长度:

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


文章来源: BackgroundWorker & Progressbar Issues c# Visual Studio 2010