我有两个文件。其中一个包含DataProgressBar类型的变量,并在加载其自己的组件之前调用startAsyncWorker()方法。另一个是下面显示的DataProgressBar。当我运行程序并在其他文件中调用startAsyncWorker()时,行为是
预期:显示带有进度栏的小窗口,在执行WorkerDatabaseInsertion中的工作时从0加载到100。然后,当完成我的第一个包含DataProgressBar的类文件时,将继续执行下一条指令。
EXPERIENCED:显示带有进度条的小窗口,未对UI进行任何更改,线程似乎冻结,因为未显示任何输出或处理证据。我关闭窗口,恢复调用文件。
public partial class DataProgressBar : Window
{
private BackgroundWorker bgw;
private String _path;
private LFPReader _lfp;
private Access db;
public DataProgressBar(String p, LFPReader reader, Access database)
{
InitializeComponent();
/* Set private class level variables */
_path = p;
_lfp = reader;
db = database;
db.open();
/* Set up worker and progressbar */
bgw = new BackgroundWorker();
SetUpWorker(bgw);
progressbar.Maximum = 100;
progressbar.Minimum = 0;
progressbar.Value = 0;
}
public void startAsyncWorker()
{
if(bgw.IsBusy != true)
{
bgw.RunWorkerAsync();
}
}
/// <summary>
/// This methods exists for completeness, but we will
/// probably not need to directly cancel the worker from here.
/// --Kurtpr
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void cancelAsyncWorker(object sender, EventArgs e)
{
if (bgw.WorkerSupportsCancellation == true)
{
bgw.CancelAsync();
}
}
private void SetUpWorker(BackgroundWorker worker)
{
worker.WorkerReportsProgress = true; // we need this in order to update the UI
worker.WorkerSupportsCancellation = true; // Not sure why, but we may need this to cancel
worker.DoWork += new DoWorkEventHandler(WorkerDatabaseInsertion);
worker.ProgressChanged += new ProgressChangedEventHandler(WorkerProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerBurnNotice);
}
private void WorkerDatabaseInsertion(object sender, DoWorkEventArgs e) {
BackgroundWorker worker = sender as BackgroundWorker;
ImageInfo ImageData = new ImageDB.ImageInfo();
double size = _lfp.GetImageData().ToArray().Length;
int index = 0;
//_path is setup in loadAsLFP() before this call.
foreach (var image in _lfp.GetImageData())
{
index++;
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
image.FullPath = Path.Combine(_path, image.FullPath);
ImageData.Add(new ImageDB(image));
db.insertImage(new ImageDB(image));
worker.ReportProgress((int)(index/size));
}
}
private void WorkerProgressChanged(object sender, ProgressChangedEventArgs e)
{
Console.WriteLine("Hello");
progressbar.Value = e.ProgressPercentage;
}
private void WorkerBurnNotice(object sender, RunWorkerCompletedEventArgs e)
{
db.close();
}
}
我怀疑我对BackgroundWorker的基本方面是错误的。我的逻辑错在哪里?
编辑这是调用和创建DataProgressBar对象的代码。
private void createDb(string filePath, LFPReader lfp)
{
//Set up LFP related objects.
ImageData = new ImageDB.ImageInfo();
//create database file.
filePath = filePath.Replace(".lfp", ".mdb").Replace(".LFP", ".mdb");
_lfpName = filePath; // update to use the database file
Access db = new Access(filePath.Replace(".lfp", ".mdb"));
db.createDatabase();
//used for calculating progress of creating this new database.
var progressbar = new DataProgressBar(_path, lfp, db);
progressbar.ShowDialog();
progressbar.startAsyncWorker();
CurImage = ImageData.First().FullPath;
//Make sure that data context is set for these images.
DataContext = ImageData;
}
最佳答案
我认为您的进度计算逻辑在这里是错误的。worker.ReportProgress((int)(index/size));
此行始终将进度报告为0。因此,进度栏将始终停留在0位置。
相反,请使用以下命令以百分比形式报告进度。worker.ReportProgress((int)(index*100/size));
更新:
您共享的代码似乎是正确的。
我认为问题出在您实现进度条的方式上。
我想您是从主线程调用startAsyncWorker方法,如下所示;
var dpb = new DataProgressBar(p, reader, database);
dpb.startAsyncWorker();
在上述两行被调用后,您的主线程应该是空闲的。
这意味着,以下代码将导致进度条冻结50秒,因为即使DoWork运行完美,UI也不会更新,因为主线程不是免费的。
var dpb = new DataProgressBar(p, reader, database);
dpb.startAsyncWorker();
Thread.Sleep(50000); //Main thread is busy for 50 seconds
更新2:
真正的问题在于以下几行:
var progressbar = new DataProgressBar(_path, lfp, db);
progressbar.ShowDialog();
progressbar.startAsyncWorker();
实际上
ShowDialog()
方法将DataProgressBar
显示为“模态”对话框。这意味着,除非您关闭该对话框,否则控件将不会转到下一行。您的问题应使用以下代码解决;
var progressbar = new DataProgressBar(_path, lfp, db);
progressbar.startAsyncWorker();
progressbar.ShowDialog();
它将首先启动您的后台工作程序,然后将显示“DataProgressBar”对话框。