gpt4 book ai didi

C# BackgroundWorker 和 ProgressBar 问题

转载 作者:太空宇宙 更新时间:2023-11-03 23:03:36 25 4
gpt4 key购买 nike

我有两个文件。一个包含 DataProgressBar 类型的变量并在加载其自己的组件之前调用 startAsyncWorker() 方法。另一个是下面显示的 DataProgressBar。当我运行我的程序并在其他文件中调用 startAsyncWorker() 时,行为

预期:显示带有进度条的小窗口,在执行 WorkerDatabaseInsertion 时从 0 加载到 100。然后,当完成包含 DataProgressBar 的第一个类文件时,将继续执行下一条指令。

已体验:显示带有进度条的小窗口,未对 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 对话框。

关于C# BackgroundWorker 和 ProgressBar 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42129391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com