进度条-在c#中使用后台工作器
本文关键字:后台 工作 | 更新日期: 2023-09-27 17:53:11
我的应用程序的一部分工作从预定义的文件夹加载图像。在这个时候,加载图像需要更多的时间。现在我发现进度条可以告诉我加载的进度。
我面临的问题是:我无法整合后台工作,进度条与我的功能。例如,下面是后台worker和进度条形码:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Load file list here
int totalImageCount = 10000;
// Set maximum value of the progress bar
progressBar1.Invoke(new Action(() => { progressBar1.Maximum = totalImageCount; }));
for (int i = 0; i < totalImageCount; i++)
{
// Load a single image here
Thread.Sleep(10);
// User cancelled loading (form shut down)
if (e.Cancel) return;
// Set the progress
progressBar1.Invoke(new Action(() => { progressBar1.Value = i; }));
}
// Cleanup here
}
// Starts the loading
private void button1_Click(object sender, EventArgs e)
{
// Start loading images
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.RunWorkerAsync();
}
// Stops the loading
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Stop the loading when the user closes the form
if (backgroundWorker1.IsBusy) backgroundWorker1.CancelAsync();
}
以下是进度条
需要的函数 private void LoadImages()
{
string imagesPath = (Application.StartupPath + "/UnknownFaces/");
string[] extensions = new[] { ".jpg", ".jpeg", ".png" };
var allfiles = Directory.GetFiles(imagesPath);
this.imageList1.ImageSize = new Size(256, 250);
this.imageList1.ColorDepth = ColorDepth.Depth32Bit;
foreach (FileInfo fileInfo in filesSorted)
{
try
{
this.imageList1.Images.Add(fileInfo.Name,
Image.FromFile(fileInfo.FullName));
}
catch
{
Console.WriteLine(fileInfo.FullName + " is is not a valid image.");
}
}
this.lstView_un.View = View.LargeIcon;
lstView_un.LargeImageList = this.imageList1;
lstView_un.Items.Clear();
for (int j = 0; j < this.imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
item.Text = imageList1.Images.Keys[j].ToString();
this.lstView_un.Items.Add(item);
}
}
catch (Exception ex)
{
MessageBox.Show("Something Wrong happen! "+ex.Message);
}
}
我认为主要的例行工作在那里:
this.lstView_un.View = View.LargeIcon;
lstView_un.LargeImageList = this.imageList1;
lstView_un.Items.Clear();
for (int j = 0; j < this.imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
item.Text = imageList1.Images.Keys[j].ToString();
this.lstView_un.Items.Add(item);
}
代码中慢的部分实际上是读取文件的循环,而不是填充ListView
的循环。
BackgroundWorker
在UI中报告或以其他方式呈现进度状态的最佳方法是使用ProgressChanged
事件。但是,您正在使用的代码示例也可以正常工作。也就是说,只需直接从工作代码更新ProgressBar
对象。它不能充分利用BackgroundWorker
提供的功能(实际上,如果你不打算使用BackgroundWorker
的功能,为什么要使用它的问题),但它会工作。
例如:var allfiles = Directory.GetFiles(imagesPath);
this.imageList1.ImageSize = new Size(256, 250);
this.imageList1.ColorDepth = ColorDepth.Depth32Bit;
// Set the maximum value based on the number of files you get
progressBar1.Invoke((MethodInvoker)(() => { progressBar1.Maximum = filesSorted.Count(); }));
foreach (FileInfo fileInfo in filesSorted)
{
try
{
this.imageList1.Images.Add(fileInfo.Name,
Image.FromFile(fileInfo.FullName));
}
catch
{
Console.WriteLine(fileInfo.FullName + " is is not a valid image.");
}
// Update the ProgressBar, incrementing by 1 for each iteration of the loop
progressBar1.Invoke((MethodInvoker)(() => progressBar1.Increment(1)));
}
注意:你的代码示例是不完整的,甚至作为一个代码片段也没有意义。一个特别的问题是,您将文件名检索到数组allfiles
中,但是您正在迭代一个完全不同的集合filesSorted
。我尽了最大的努力使用您提供的代码,但是由于您发布的代码不能按原样工作,您可能不得不对我提供的示例进行一些小的调整,以使它做您想要的。
如果你不能用上面的方法解决这个问题,请提供一个好的,最小的, 完整的代码示例,可靠地说明你的场景和你到底有什么问题。