BackgroundWorker 不会触发 RunWorkerCompleted 事件
我不是在 Windows 窗体中创建后台工作程序,而是在实现所有处理的类文件(BusinessLogic)中创建后台工作程序。在主窗体中,我首先调用初始化 BGW 的 BL 方法。然后我调用 BL 的方法来启动 BGW。
这是我的实现的更多背景:)。 如何在类文件中使用 BackGroundWorker?
DoWork 事件运行正常,但是它不会调用 RunWorkerCompleted。
一些谷歌搜索,我发现了这个链接。我感觉我的问题和这些人一样。 http://www.eggheadcafe.com/software/aspnet/29191764/backgroundworker-does-not-fire-the-runworkercompleted-event.aspx
如果您对此问题有任何意见,我将不胜感激。提前致谢。
主窗体中的代码:
private void frmMain_Load(object sender, EventArgs e)
{
Hide();
BusinessLogic.BGWInitialize();
BusinessLogic.StartBackgroundWorker();
while (!BusinessLogic.firstCycleDone)
{
Thread.Sleep(100);
}
Show();
}
业务逻辑中的代码:
public static void BGWInitialize()
{
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged);
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
bgWorker.WorkerReportsProgress = true;
}
public static void StartBackgroundWorker()
{
bgWorker.RunWorkerAsync();
}
private static void bgWorker_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
firstCycleDone = true;
}
I'm creating backgroundworker not in my windows form but in the class file (BusinessLogic) that implements all the processing. From main form I first call the BL method that initializes the BGW. Then I call the method of BL which will start the BGW.
Here is more background :) on my implementation.
How to use BackGroundWorker in class file?
The DoWork event runs fine but it doesnt call the RunWorkerCompleted.
Some googling and I found out this link. I've a feeling that my problem is same as this guys.
http://www.eggheadcafe.com/software/aspnet/29191764/backgroundworker-does-not-fire-the-runworkercompleted-event.aspx
I'd appreciate any input on this issue. Thanks in advance.
Code in Main form:
private void frmMain_Load(object sender, EventArgs e)
{
Hide();
BusinessLogic.BGWInitialize();
BusinessLogic.StartBackgroundWorker();
while (!BusinessLogic.firstCycleDone)
{
Thread.Sleep(100);
}
Show();
}
Code in BusinessLogic:
public static void BGWInitialize()
{
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged);
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
bgWorker.WorkerReportsProgress = true;
}
public static void StartBackgroundWorker()
{
bgWorker.RunWorkerAsync();
}
private static void bgWorker_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
firstCycleDone = true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完成的事件被调用到主线程。它应该由 MessagePump 拾取并执行。
但是,您的等待和睡眠代码正在阻塞消息循环。
这里的答案是您没有使用Backgroundworker或其他形式的线程...
只需直接调用
bgWorker_DoWork()
:The completed event is Invoked to the main thread. It is supposed to be picked up and executed by the MessagePump.
However, your Wait-and-Sleep code is blocking the message loop.
The answer here is that you have no use for a Backgroundworker or another form of threading...
Just call
bgWorker_DoWork()
directly:如果您只调用
Application.DoEvents();
而不是Sleep(100);
您的代码就可以工作,但正如我之前所说的BackgroundWorker
类是一个有缺陷的东西,我个人会使用我自己的线程和报告或者你可以睡一会儿,然后调用
DoEvents
if you would just call
Application.DoEvents();
instead ofSleep(100);
your code would work, but as I said previouslyBackgroundWorker
class is a buggy thing and I would personally use my own threads and reportsAlternatively you could sleep a bit and then call
DoEvents