如何在嵌套的foreach循环中使用ProgressBar?

时间:2013-11-08 07:27:49

标签: c# foreach progress-bar nested-loops

如何在嵌套的foreach循环中正确使用Progress Bar? 因为我这样做了,我在foreach循环过程中的进程只能完成我正在比较的第二个文件。

int count = d1.GetFiles(fd.fileType, SearchOption.AllDirectories).Length;
int current = 0;
foreach(...)
{
    foreach(...)
    {
      //process
    }
current++;
pbSearch.Value = current / count * 30 + 70;
label1.Text = pbSearch.Value.ToString();
}

1 个答案:

答案 0 :(得分:1)

根据您的两个foreach循环,您可以考虑使用两个进度条或重新计算最大值,请查看此MSDN示例:

    private void CopyWithProgress(string[] filenames)
    {
        // Display the ProgressBar control.
        pBar1.Visible = true;
        // Set Minimum to 1 to represent the first file being copied.
        pBar1.Minimum = 1;
        // Set Maximum to the total number of files to copy.
        pBar1.Maximum = filenames.Length;
        // Set the initial value of the ProgressBar.
        pBar1.Value = 1;
        // Set the Step property to a value of 1 to represent each file being copied.
        pBar1.Step = 1;

        // Loop through all files to copy. 
        for (int x = 1; x <= filenames.Length; x++)
        {
            // Copy the file and increment the ProgressBar if successful. 
            if(CopyFile(filenames[x-1]) == true)
            {
                // Perform the increment on the ProgressBar.
                pBar1.PerformStep();
            }
        }
    }