System.ComponentModel.BackgroundWorker 从不调用 ProgressChanged

发布于 2024-10-05 04:50:40 字数 1377 浏览 4 评论 0原文

我最近重写了我的 Windows 窗体应用程序以使用 BackgroundWorker 实例,而不是使用手动创建的“工作线程”。签入后我注意到一些测试开始失败。经过一些调试后,我可以通过向您展示以下 2 个测试来证明我的问题:

    [Test]        
    public void Test_A()
    { 
        bool progressChanged = false;
        var worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.DoWork += (s, e) => worker.ReportProgress(0, null);
        worker.ProgressChanged += (s, e) => progressChanged = true;
        worker.RunWorkerAsync();
        Thread.Sleep(100);
        progressChanged.ShouldBeTrue();            
    }

    [Test]        
    public void Test_B()
    { 
        //Creation of o form component causes (?) this test to fail, even do I dispose it
        var view = new Form();
        view.Dispose();

        bool progressChanged = false;
        var worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.DoWork += (s, e) => worker.ReportProgress(0, null);
        worker.ProgressChanged += (s, e) => progressChanged = true;
        worker.RunWorkerAsync();
        Thread.Sleep(100);
        progressChanged.ShouldBeTrue();            
    }

Test_A 成功,而 Test_B 失败。这与我睡 100 毫秒还是 100 分钟无关。为什么??我的生产代码似乎可以工作,但令人烦恼的是无法拥有有效的回归测试套件(我还有其他创建表单组件的测试,并且这些测试必须在使用BackgroundWorker进行测试之前执行) 下一步是检查BackgroundWorker的源代码,但在这之前我想我应该在这里检查一下帮助。

问候迈克尔

I recently rewrote my Windows Forms application to use BackgroundWorker instances instead of using manually created "worker threads". After checkin I noticed some tests started to fail. After some debugging I can demonstate my problems by showing you the following 2 tests:

    [Test]        
    public void Test_A()
    { 
        bool progressChanged = false;
        var worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.DoWork += (s, e) => worker.ReportProgress(0, null);
        worker.ProgressChanged += (s, e) => progressChanged = true;
        worker.RunWorkerAsync();
        Thread.Sleep(100);
        progressChanged.ShouldBeTrue();            
    }

    [Test]        
    public void Test_B()
    { 
        //Creation of o form component causes (?) this test to fail, even do I dispose it
        var view = new Form();
        view.Dispose();

        bool progressChanged = false;
        var worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.DoWork += (s, e) => worker.ReportProgress(0, null);
        worker.ProgressChanged += (s, e) => progressChanged = true;
        worker.RunWorkerAsync();
        Thread.Sleep(100);
        progressChanged.ShouldBeTrue();            
    }

Test_A success while Test_B fails. This is regardless if I sleep 100 ms or 100 minutes. Why?? My production code seem to work though, but it is annoying not being able to have a regression test suite that works (I have other test creating Forms components and these tests must be executed BEFORE my test using BackgroundWorker)
The next step would be to examine the source code of BackgroundWorker, but before I do that I thought I`d check for help here.

Regards Michael

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

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

发布评论

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

评论(1

葵雨 2024-10-12 04:50:40

在其他任何事情之前添加

WindowsFormsSynchronizationContext.AutoInstall = false;

In Test_B() 。 BackgroundWorker 非常面向 UI(即:Winforms),它会疯狂猜测如何同步事物,但并不像您想要的那样。看看这里的一些解释(尤其是来自 Scott Berry 的解释):

Add

WindowsFormsSynchronizationContext.AutoInstall = false;

In Test_B() before anything else. The BackgroundWorker, wich is very UI (ie: Winforms) oriented, is wild guessing how to sync things, but not like you want. Have a look here for some explanation (especially from Scott Berry): WindowsFormsSynchronizationContext

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