BackGroundWorker 无法识别 DoWork 的完成

发布于 2024-11-08 15:44:20 字数 580 浏览 0 评论 0 原文

我有一个程序,我想将多个任务按顺序传递给 BackGroundWorker。我的问题是 RunWorkerCompleted 事件没有被执行并且 IsBusy 标志没有被清除。 ProgressChanged 事件确实执行。 DoWork 例程中的最后一条语句确实被执行。我可以完成第一个任务,但不能完成任何后续任务。

有什么想法要看什么吗?

编辑(添加代码):

概念(稍微简化)是这样的:

for (int i = 0; i < cnt; i++)
            {
                string sDiv = dlg.GetItem(i).Substring(0, 5);
                bgw8Div.RunWorkerAsync(sDiv);
                // some kind of wait or delay or test needed here
            }

如果 cnt==1,一切都很好。如果 cnt>1,则无论我在循环中放置多少等待或延迟,对于情况 i==0 都不会执行 RunWorkerCompleted 事件。

I have a program in which I want to pass multiple tasks to a BackGroundWorker in sequence. My problem is that the RunWorkerCompleted event is not being executed and the IsBusy flag is not being cleared. The ProgressChanged event does execute. The final statement in the DoWork routine does get executed. I can do the first task, but not any subsequent tasks.

Any ideas what to look at?

Edit (code added):

The notion (somewhat simplified) is like this:

for (int i = 0; i < cnt; i++)
            {
                string sDiv = dlg.GetItem(i).Substring(0, 5);
                bgw8Div.RunWorkerAsync(sDiv);
                // some kind of wait or delay or test needed here
            }

If cnt==1, all is well. If cnt>1, the RunWorkerCompleted event is never executed for case i==0 regardless of the wait or delay that I put in the loop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

Saygoodbye 2024-11-15 15:44:20

您必须以编程方式创建BackgroundWorker:

for (int i = 0; i < cnt; i++)
        {
            string sDiv = dlg.GetItem(i).Substring(0, 5);
            var bgw = new BackGroundWorker();
            // some kind of wait or delay or test needed here
            bgw.DoWork += 
            new DoWorkEventHandler(bw_DoWork);
            bgw.ProgressChanged += 
            new ProgressChangedEventHandler(bw_ProgressChanged);
            bgw.RunWorkerCompleted += 
            new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            bgw.RunWorkerAsync(sDiv);
        }  

另一个选择是等待bgw8Div 完成。您可以在 如何等待要取消的BackgroundWorker?。当然,这样您就会失去多线程功能。

You have to create BackgroundWorker programmatically:

for (int i = 0; i < cnt; i++)
        {
            string sDiv = dlg.GetItem(i).Substring(0, 5);
            var bgw = new BackGroundWorker();
            // some kind of wait or delay or test needed here
            bgw.DoWork += 
            new DoWorkEventHandler(bw_DoWork);
            bgw.ProgressChanged += 
            new ProgressChangedEventHandler(bw_ProgressChanged);
            bgw.RunWorkerCompleted += 
            new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            bgw.RunWorkerAsync(sDiv);
        }  

Another option is to wait until your bgw8Div is completed. You can find more info at How to wait for a BackgroundWorker to cancel?. Of course, you'll lose the multi-threading features this way.

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