BackgroundWorker 不会触发 RunWorkerCompleted 事件

发布于 2024-11-19 16:44:25 字数 1696 浏览 2 评论 0原文

我不是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只等公子 2024-11-26 16:44:25

完成的事件被调用到主线程。它应该由 MessagePump 拾取并执行。

但是,您的等待和睡眠代码正在阻塞消息循环。

  Hide();
  ....
  while (!BusinessLogic.firstCycleDone)
  {
    Thread.Sleep(100);
  }
  Show();

这里的答案是您没有使用Backgroundworker或其他形式的线程...

只需直接调用bgWorker_DoWork()

 // Hide();
 bgWorker_DoWork();  // rename
 Show();  

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.

  Hide();
  ....
  while (!BusinessLogic.firstCycleDone)
  {
    Thread.Sleep(100);
  }
  Show();

The answer here is that you have no use for a Backgroundworker or another form of threading...

Just call bgWorker_DoWork() directly:

 // Hide();
 bgWorker_DoWork();  // rename
 Show();  
安静 2024-11-26 16:44:25

如果您只调用 Application.DoEvents(); 而不是 Sleep(100); 您的代码就可以工作,但正如我之前所说的 BackgroundWorker类是一个有缺陷的东西,我个人会使用我自己的线程和报告

或者你可以睡一会儿,然后调用DoEvents

if you would just call Application.DoEvents(); instead of Sleep(100); your code would work, but as I said previously BackgroundWorker class is a buggy thing and I would personally use my own threads and reports

Alternatively you could sleep a bit and then call DoEvents

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文