您必须将其放在新线程上,而不是主线程上。
这是一个小例子:
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
}
The Dispacher.Invoke
是因为它在WPF上,对于WinForm只需将其更改为this.Invoke
在这个例子中,当单击按钮时,BackgroundWorker 将启动,有一个从 1 到 100 的 for,它将休眠 100 毫秒并更新进度条。
希望这会有所帮助
Edit
现在在示例中包含了当BackgroundWorker完成时要运行的事件,以防万一需要它
Edit 2:
我的建议是,在搜索背景上的照片时,将它们全部插入到 DataTable 中,因为您已经准备好搜索它们,并且工作也可以在这里完成,然后只需在 FormResultadosFotos 上创建一个将接收该 DataTable 的构造函数即可。
据我了解,主要目标是在 FormPesquisaFotos 表单上搜索它们(这就是为什么我们在那里有后台工作人员,搜索它们并更新 ProgressBar)并在新表单 AKA FormResultadosFotos 上显示它们
// Lets create a DataTable variable to be access on the Worker_DoWork and then on the Worker_RunWorkerCompleted
private DataTable tableOfPhotos;
private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
// Search for the photos here and then add them to the DataTable
this.tableOfPhotos = new DataTable();
tableOfPhotos.Columns.Add("Nome e formato do ficheiro (duplo clique para visualizar a imagem)");
tableOfPhotos.Columns.Add("Caminho ( pode ser copiado Ctrl+C )");
foreach (string diretorio in diretorios)
{
// se pretendermos pesquisar em várias pastas
List<string> diretorios = new List<string>()
{@"\\Server\folder01\folder02"};
// se pretendermos pesquisar as várias extensões
List<string> extensoes = new List<string>()
{"*.jpg","*.bmp","*.png","*.tiff","*.gif"};
var ficheiros = Directory.EnumerateFiles(diretorio, "*", SearchOption.AllDirectories).
Where(r => extensoes.Contains(Path.GetExtension(r.ToLower())));
foreach (var ficheiro in ficheiros)
{
DataRow row = tableOfPhotos.NewRow();
row[0] = Path.GetFileName(ficheiro);
row[1] = ficheiro;
tableOfPhotos.Rows.Add(row);
}
}
}
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// You can put the code here to open the new form and such
FormResultadosFotos NovoForm = new FormResultadosFotos(this.tableOfPhotos);
NovoForm.Show();
}
// Constructor that will receive the DataTable and put it into the dataGridView1, it should be added on the Form FormResultadosFotos
Public FormResultadosFotos(DataTable table)
{
InitializeComponent();
// In here we will tell the where is the source for the dataGridView1
this.dataGridView1.DataSource = table;
}
在这里,您还可以通过在行上放置断点来查看该表带来的内容this.dataGridView1.DataSource = table;
,如果表是空的,则表中没有引入任何内容(目录中可能没有照片?无法访问它?不在工作并且我没有任何 IDE,只是根据我所拥有的内容进行回答我的头脑,但如果需要,您也可以通过类似代码获取文件:
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);
}
Edit 3:
更改您的代码
List<string> extensoes = new List<string>(){".jpg",".bmp",".png",".tiff",".gif"};
to
List<string> extensoes = new List<string>(){"*.jpg","*.bmp","*.png","*.tiff","*.gif"};
您需要在扩展名前添加 *(例如 .png)来搜索该扩展名的文件