ProgressBar数据加载

时间:2018-08-28 11:16:46

标签: c# photo

我正在尝试开发一个应用程序,该应用程序允许我搜索照片,在DataGridView中列出并在PictureBox中进行预览。
现在,我必须指出的目录存在问题。如果我在两个Forms中都放置了相同的目录,则ProgressBar没有结尾,搜索结果也不会出现。
但是,如果我从FormProcuraFotos Form中删除路径,则结果已经出现,但是ProgressBar不能正常工作。

为了简化流程,我必须更改哪些代码?

FormProcuraFotos代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _myprog
{
    public partial class FormProcuraFotos : Form
    {
        public FormProcuraFotos()
        {
            InitializeComponent();
        }
        // We create the DataTable here so we can create the new inside the Worker_DoWork and use it also on the Worker_RunWorkerCompleted
        DataTable tableWithPhotos;
        private void button1_Click(object sender, EventArgs e)
        {
            // Make the progressBar1 to look like its allways loading something
            progressBar1.Style = ProgressBarStyle.Marquee;
            // Make it here visible
            progressBar1.Visible = true;
            var worker = new BackgroundWorker();

            // Event that runs on background
            worker.DoWork += this.Worker_DoWork;

            // Event that will run after the background event as finnished
            worker.RunWorkerCompleted += this.Worker_RunWorkerCompleted;
            worker.RunWorkerAsync();
        }

        // The reason for having this here was to work with the progress bar and to search for the photos and it will not block the UI Thread
        // My advice is to have them here and pass them to the next form with a constructor
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            // We must create a list for all the files that the search it will find
            List<string> filesList = new List<string>();
            // Create the new DataTable to be used
            tableWithPhotos = new DataTable();
            tableWithPhotos.Columns.Add("Nome e formato do ficheiro (duplo clique para visualizar a imagem)");
            tableWithPhotos.Columns.Add("Caminho ( pode ser copiado Ctrl+C )");

            // What folders that we want to search for the files
            var diretorios = new List<string>() {@"\\Server\folder1\folder2"};

            // What extensions that we want to search
            var extensoes = new List<string>() { "*.jpg", "*.bmp", "*.png", "*.tiff", "*.gif" };

            // This 2 foreach are to search for the files with the extension that is on the extensoes and on all directories that are on diretorios
            // In for foreach we go through all the extensions that we want to search
            foreach (string entryExtensions in extensoes)
            {
                // Now we must go through all the directories to search for the extension that is on the entryExtensions
                foreach (string entryDirectory in diretorios)
                {
                    // SearchOption.AllDirectories search the directory and sub directorys if necessary
                    // SearchOption.TopDirectoryOnly search only the directory
                    filesList.AddRange(Directory.GetFiles(entryDirectory, entryExtensions, SearchOption.AllDirectories));
                }
            }

            // And now here we will add all the files that it has found into the DataTable
            foreach (string entryFiles in filesList)
            {
                DataRow row = tableWithPhotos.NewRow();
                row[0] = Path.GetFileName(entryFiles);
                row[1] = entryFiles;
                tableWithPhotos.Rows.Add(row);
            }
        }
        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // With the new constructor on the FormResultadosFotos, we pass the table like this so the form can receive it
            progressBar1.Visible = false;
            var NovoForm = new FormResultadosFotos(tableWithPhotos);
            NovoForm.Show();
        }
    }
}

FormResultadosFotos代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace _myprog
{
    public partial class FormResultadosFotos : Form
    {
        // This is the constructor that we have added to the FormResultadosFotos so it can receive the DataTable that was created on the previous form
        public FormResultadosFotos(DataTable table)
        {
            InitializeComponent();
            dataGridView1.DataSource = table;
            dataGridView1.Columns[1].Visible = true;
            // What can be done here to not block the UI thread if is being blocked while populating the dataGridView1, is to create another BackgroundWorker here and populate the dataGridView1 there
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            var myForm = new FormPictureBox();
            string imageName = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            var img = Image.FromFile(imageName);

            myForm.pictureBox1.Image = img;
            myForm.ShowDialog();
        }
        public FormResultadosFotos()
        {
            InitializeComponent();
        }
        private void FormFotos_Load(object sender, EventArgs e)
        {
            // se pretendermos pesquisar em várias pastas
            List<string> diretorios = new List<string>()
            {@"\\server\folder1\folder2"};

            // se pretendermos pesquisar as várias extensões
            List<string> extensoes = new List<string>()
            {".jpg",".bmp",".png",".tiff",".gif"};

            DataTable table = new DataTable();
            table.Columns.Add("Nome e formato do ficheiro (duplo clique para visualizar a imagem)");
            table.Columns.Add("Caminho ( pode ser copiado Ctrl+C )");
            foreach (string diretorio in diretorios)
            {
                var ficheiros = Directory.EnumerateFiles(diretorio, "*", SearchOption.AllDirectories).
                    Where(r => extensoes.Contains(Path.GetExtension(r.ToLower())));

                foreach (var ficheiro in ficheiros)
                {
                    DataRow row = table.NewRow();
                    row[0] = Path.GetFileName(ficheiro);
                    row[1] = ficheiro;
                    table.Rows.Add(row);
                }
            }
            dataGridView1.DataSource = table;
            dataGridView1.Columns[1].Visible = true;
        }
    }
}

谢谢。

1 个答案:

答案 0 :(得分:0)

好吧,您缺少完整的进度处理:

worker.WorkerReportsProgress = true;
worker.ProgressChanged += this.Worker_ProgressChanged;

然后:

private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    this.progressBar1.Value = e.ProgressPercentage;
}

最后,您需要在DoWork方法中调用它:

var worker = sender as BackgroundWorker;

// this is 50%, you need to find out where and with which percentage to call
worker.ReportProgress(50);