C# 搜索按钮上的进度条

C# 搜索按钮上的进度条,c#,multithreading,winforms,progress-bar,C#,Multithreading,Winforms,Progress Bar,我用这个c代码来显示进度条: { public partial class FormPesquisaFotos : Form { public FormPesquisaFotos() { InitializeComponent(); } private void FormPesquisaFotos_Load(object sender, EventArgs e) {

我用这个c代码来显示进度条:

{
    public partial class FormPesquisaFotos : Form
    {
        public FormPesquisaFotos()
        {
            InitializeComponent();
        }

        private void FormPesquisaFotos_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Mostra a barra de progresso da pesquisa
            while (progressBar1.Value < 100)
            progressBar1.Value += 1;
            {
                //Criar um objeto (instância, cópia) da classe FormResultadosFotos
                FormResultadosFotos NovoForm = new FormResultadosFotos();
                NovoForm.Show();
            }
        }
    }
}
{
公共部分类FormPesquisaFotos:Form
{
公共表单pesquisafotos()
{
初始化组件();
}
私有void FormPesquisaFotos_加载(对象发送方,事件参数e)
{
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
//莫斯特拉·巴拉·德普雷索·达佩斯奎萨
而(progressBar1.0值<100)
progressBar1.值+=1;
{
//cópia省的研究所(研究所)的研究结果
FormResultadosFotos NovoForm=新的FormResultadosFotos();
NovoForm.Show();
}
}
}
}
它仅在搜索结束时加载(单击按钮后)。如何在搜索开始时运行progressbar

下面是在新表单上显示结果的代码。Progressbar在95%时停止,几秒钟后显示结果

{
公共部分类FormResultadosFotos:Form
{
公共表单resultadosfotos()
{
初始化组件();
}
私有void FormFotos_加载(对象发送方,事件参数e)
{
//这是一个很好的例子
List diretorios=新列表()
{@“\\Server\folder01\folder02”};
//当várias延伸时,他们假装是佩斯奎萨尔
List extensoes=新列表()
{.jpg“,.bmp“,.png“,.tiff“,.gif”};
DataTable=新的DataTable();
表.列.添加(“Nome e formato do ficheiro(duplo clique para visualizar a imagem)”;
表.列.添加(“Camino(pode ser copiado Ctrl+C)”;
foreach(diretorios中的字符串diretorio)
{
var ficheiros=Directory.EnumerateFiles(diretorio,“*”,SearchOption.AllDirectories)。
其中(r=>extensoes.Contains(Path.GetExtension(r.ToLower())));
foreach(在ficheiros中为var ficheiro)
{
DataRow行=table.NewRow();
行[0]=Path.GetFileName(ficheiro);
第[1]行=菲切罗;
table.Rows.Add(行);
}
}
dataGridView1.DataSource=表;
dataGridView1.Columns[1]。Visible=true;
}
私有void dataGridView1双击(对象发送方,事件参数e)
{
FormPictureBox myForm=新FormPictureBox();
字符串imageName=dataGridView1.CurrentRow.Cells[1].Value.ToString();
图像img;
img=Image.FromFile(imageName);
myForm.pictureBox1.Image=img;
myForm.ShowDialog();
}
}

}
您在这里描述的是一个多线程问题。 您的循环在UI有机会自我更新之前运行

您应该查看一个解释&关于如何在循环中更新UI的示例


关于

您必须将其放在新线程上,而不是主线程上

下面是一个小例子:

private void buttonWorkerTest_Click(object sender, RoutedEventArgs e)
{
    this.progressBarWorkerTest.Value = 0;
    BackgroundWorker worker = new BackgroundWorker();
    // Event for the method that will run on the background
    worker.DoWork += this.Worker_DoWork;
    // Event that will run after the BackgroundWorker finnish
    worker.RunWorkerCompleted += this.Worker_RunWorkerCompleted;
    worker.RunWorkerAsync();


}

private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = 1; i <= 100; i++)
    {
        Dispatcher.Invoke(new Action(() =>
        {
            this.progressBarWorkerTest.Value = i;
        }));
        Thread.Sleep(100);
    }
}

private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // You can put the code here to open the new form and such
}
在这里,您还可以通过在
this.dataGridView1.DataSource=table行上放置断点来查看表带来了什么,如果表是空的,则表中没有任何内容(可能目录中没有照片?无法访问它?不在工作中,也没有IDE,只是根据我头脑中的内容创建我的anwser,但如果需要,您也可以使用模拟代码获取文件:

List<string> tempList = new List<string>;
foreach (string entryExt in extensoes)
{
    foreach (string entryDir in diretorios)
    {
        //  SearchOption.AllDirectories search the directory and sub directorys if necessary
        // SearchOption.TopDirectoryOnly search only the directory
        tempList.AddRange(Directory.GetFiles(entryDir, entryExt, SearchOption.AllDirectories));
    }
}

// Here would run all the files that it has found and add them into the DataTable
foreach (string entry in tempList)
{
    DataRow row = tableOfPhotos.NewRow();
    row[0] = Path.GetFileName(entry);
    row[1] = entry;
    tableOfPhotos.Rows.Add(row);
}
List templast=新列表;
foreach(extenses中的字符串entryExt)
{
foreach(diretorios中的字符串entryDir)
{
//SearchOption.AllDirectories在必要时搜索目录和子目录
//SearchOption.TopDirectoryOnly仅搜索目录
AddRange(Directory.GetFiles(entryDir、entryExt、SearchOption.AllDirectories));
}
}
//这里将运行找到的所有文件,并将它们添加到DataTable中
foreach(模板列表中的字符串条目)
{
DataRow row=tableOfPhotos.NewRow();
行[0]=Path.GetFileName(条目);
第[1]行=条目;
tableOfPhotos.Rows.Add(row);
}
编辑3:

更改您的代码

List<string> extensoes = new List<string>(){".jpg",".bmp",".png",".tiff",".gif"};
List extensoes=new List(){.jpg“,.bmp“,.png“,.tiff“,.gif”};

List extensoes=new List(){“*.jpg”、“*.bmp”、“*.png”、“*.tiff”、“*.gif”};

您需要在扩展名(.png作为示例)之前使用*来搜索该扩展名的文件

如果扩展名多(或少)怎么办超过100张照片?您的进度条只会在找到照片时增加其值。并且…您将向用户显示大约100张表单?此外,您的进度条不会更新,因为您正在用户线程中检索照片。当事件完成处理时,屏幕将更新。搜索结果显示在包含图片的datagridview中turebox@mnieto:while不包括表单创建。它只增加了值。谢谢@PalleDue,我没有看到打开的{is after
progressBar1.value+=1;
这是一个奇怪的结构,在这种情况下,花括号是多余的(而且令人困惑)您的代码非常不清楚,循环是如何工作的?为什么它是一个多线程问题?您是如何得出这个结论的?因为您的按钮单击在UI线程上运行,因此您的循环在UI线程中实现,因此当您使用该线程执行l时,UI线程没有执行进度条更新oop:3太好了!进度条加载得很好,但最后不显示结果(//Criar um objeto(inst–ncia,cópia)da classe FormResultadosFotos FormResultadosFotos NovoForm=new FormResultadosFotos();NovoForm.show();)当您将新表单放入
Worker\R中时,您能用该部分更完整的代码更新问题吗?这样我们就可以看到问题所在了
List<string> extensoes = new List<string>(){".jpg",".bmp",".png",".tiff",".gif"};
List<string> extensoes = new List<string>(){"*.jpg","*.bmp","*.png","*.tiff","*.gif"};