如何使用后台工作者以正确的方式报告文件图像转换的进度
本文关键字:文件 报告 图像 转换 方式 工作者 后台 何使用 | 更新日期: 2023-09-27 18:21:00
private int FilesNamesCounter = 0;
private String FileName = "";
private List<string> myGifs = new List<string>();
private bool cancelop = false;
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgw = (BackgroundWorker)sender;
int Counter = 0;
int percentage = 0;
int total = allfiles.Count;
for (int i = 0; i < allfiles.Count; i++)
{
if (bgw.CancellationPending == true)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = Properties.Resources.Weather_Michmoret;
e.Cancel = true;
makeGif = false;
cancelop = true;
timer1.Stop();
break;
}
else
{
timer1.Start();
Counter += 1;
// calculating percentage and report it
percentage = Counter * 100 / total;
bgw.ReportProgress(percentage);
converttogif();
}
}
if (makeGif == true)
{
FilesNamesCounter += 1;
unfreez.MakeGIF(myGifs, outputfile + FilesNamesCounter + ".gif", 80, true);
}
bgw.ReportProgress(100);
e.Result = allfiles;
}
转换方法:
private void converttogif()
{
DirectoryInfo dirinfo = new DirectoryInfo(selectedfilesdirectoryName);
FileInfo[] gifFileInfo = dirinfo.GetFiles("*.gif");
Image gifImage;
for (int i = 0; i < allfiles.Count; i++)
{
FileName = allfiles[i];
gifImage = Image.FromFile(gifFileInfo[i].FullName);
gifImage.Save(FileName, System.Drawing.Imaging.ImageFormat.Gif);
gifImage.Dispose();
myGifs.Add(FileName);
}
}
进度更改事件:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
}
已完成的活动:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
button2.Enabled = false;
lblStatus.Text = "Task Cancelled.";
}
else if (!(e.Error == null))
{
lblStatus.Text = "Error while performing background operation.";
}
else
{
lblStatus.Text = "Task Completed...";
allfiles = (List<string>)e.Result;
timer1.Stop();
button1.Enabled = false;
progressBar1.BackColor = Color.FromArgb(0, 211, 040);
displaylastanimatedgif();
}
}
问题出在 DoWork 事件中的转换 gif 方法上。它将循环循环,并且每次都会一遍又一遍地循环转换 gif 中,因为我已经在 dowork 中的图像上循环了。
我想做的是以转换进度的百分比向进度栏 1 报告进度。例如,如果我在所有文件中都有 285 个要转换的文件图像,然后转换它们一次并报告每个文件将进度百分比转换为进度栏,因此整个转换应该在进度栏中从 0% 到 100% 开始,或者从 1% 到 100% 开始是合乎逻辑的。
但是报告和显示转换进度的想法。
更新
这是我现在尝试的:
private int FilesNamesCounter = 0;
private String FileName = "";
private List<string> myGifs = new List<string>();
private bool cancelop = false;
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgw = (BackgroundWorker)sender;
converttogif(bgw);
bgw.ReportProgress(100);
e.Result = allfiles;
}
private void converttogif(BackgroundWorker bgw)
{
int Counter = 0;
int percentage = 0;
int total = allfiles.Count;
DirectoryInfo dirinfo = new DirectoryInfo(selectedfilesdirectoryName);
FileInfo[] gifFileInfo = dirinfo.GetFiles("*.gif");
Image gifImage;
for (int i = 0; i < allfiles.Count; i++)
{
FileName = allfiles[i];
gifImage = Image.FromFile(gifFileInfo[i].FullName);
gifImage.Save(FileName, System.Drawing.Imaging.ImageFormat.Gif);
gifImage.Dispose();
myGifs.Add(FileName);
Counter += 1;
// calculating percentage and report it
percentage = Counter * 100 / total;
bgw.ReportProgress(percentage);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
button2.Enabled = false;
lblStatus.Text = "Task Cancelled.";
}
else if (!(e.Error == null))
{
lblStatus.Text = "Error while performing background operation.";
}
else
{
if (makeGif == true)
{
FilesNamesCounter += 1;
unfreez.MakeGIF(myGifs, outputfile + FilesNamesCounter + ".gif", 80, true);
}
lblStatus.Text = "Task Completed...";
allfiles = (List<string>)e.Result;
timer1.Stop();
button1.Enabled = false;
progressBar1.BackColor = Color.FromArgb(0, 211, 040);
displaylastanimatedgif();
}
}
现在的问题是它达到了 99%,并且在进度条结束之前很久,就像 3/4 一样,但显示 99%,然后它正在等待我使用断点,它正在到达创建动画 gif 的已完成事件然后显示 100% 和进度条到达最后。
我该如何解决它?
简单但不好的方法:将后台工作线程传递给您的 converttogif 函数,并在 cicle 中调用 bgw.ReportProgress(percentage);
。
更好的解决方案是让你的converttoGif
成为不同类的一部分,引发它自己的"进度"事件,你可以注册到,并在事件处理程序中调用bgw.ReportProgress
这将使 gifconversion 代码与表单及其后台工作线程隔离,使表单需要注册转换类事件。