为什么在启动backgroundworker时,列表中的文件数与报告文件的数量不同?

时间:2017-09-17 17:26:32

标签: c# .net wpf

添加图片时,我将它们添加到列表中并更新标签:

private void Add_Files_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Multiselect = true;
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            openFileDialog.Filter = "All Images Files (*.png;*.jpeg;*.gif;*.jpg;*.bmp;*.tiff;*.tif)|*.png;*.jpeg;*.gif;*.jpg;*.bmp;*.tiff;*.tif" +
            "|PNG Portable Network Graphics (*.png)|*.png" +
            "|JPEG File Interchange Format (*.jpg *.jpeg *jfif)|*.jpg;*.jpeg;*.jfif" +
            "|BMP Windows Bitmap (*.bmp)|*.bmp" +
            "|TIF Tagged Imaged File Format (*.tif *.tiff)|*.tif;*.tiff" +
            "|GIF Graphics Interchange Format (*.gif)|*.gif";

            if (openFileDialog.ShowDialog() == true)
            {
                int i = 0;
                foreach (string filename in openFileDialog.FileNames)
                {
                    directories.Add(filename);
                    listView.Items.Add(System.IO.Path.GetFileName(filename));
                    i++;
                    label2.Content = i.ToString();
                    System.Windows.Forms.Application.DoEvents();
                    Thread.Sleep(3);
                }
            }
        }

例如,现在在List目录中有46个文件,label2也显示46。

现在我点击开始:

private void Start_Click(object sender, RoutedEventArgs e)
        {
            if (listView.Items.Count > 0)
                worker.RunWorkerAsync();
        }

在dowork活动中,我在报告过程中报告了百分比和文件数量:

private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < directories.Count; i++)
            {
                BitmapImage imagetosave = ResizeImage(directories[i]);
                Save(imagetosave, saveDirectory);

                int percents = ((i + 1) * 100) / directories.Count;
                worker.ReportProgress(percents, i);              
            }
        }

在progresschanged事件中:

private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            label.Content = e.ProgressPercentage.ToString() + "%";
            label2.Content = e.UserState;
            listView.Items.RemoveAt((int)e.UserState);
            listView.Items.Insert((int)e.UserState, "Resized and saved");
        }

在流程结束时,label2将显示45而不是46。 它应该显示已处理的文件数量。

一般情况下,我想让数字从高到低,例如从46到0.现在,我只是首先显示用户添加的文件数量,然后显示处理了多少文件但是#&# 39; s显示45而不是46。

1 个答案:

答案 0 :(得分:1)

  

现在我只是首先显示用户添加了多少文件,然后显示已处理的文件数量,但它显示的是45而不是46。

这是因为您将i作为userState传递给ReportProgress方法。它应该是i + 1

private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = 0; i < directories.Count; i++)
    {
        BitmapImage imagetosave = ResizeImage(directories[i]);
        Save(imagetosave, saveDirectory);
        int x = i + 1;
        int percents = (x * 100) / directories.Count;
        worker.ReportProgress(percents, x);
    }
}
  

一般来说,我想让数字从高到低,例如从46到0

传递directories.Count - (i + 1)然后:

private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = 0; i < directories.Count; i++)
    {
        ...
        int x = i + 1;
        worker.ReportProgress(percents, directories.Count - x);
    }
}