System.ComponentModel.BackgroundWorker 从不调用 ProgressChanged
我最近重写了我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在其他任何事情之前添加
In Test_B() 。 BackgroundWorker 非常面向 UI(即:Winforms),它会疯狂猜测如何同步事物,但并不像您想要的那样。看看这里的一些解释(尤其是来自 Scott Berry 的解释):
Add
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