如何让BackgroundWorker ProgressChanged事件按顺序执行?
考虑以下代码:
private static BackgroundWorker bg = new BackgroundWorker();
static void Main(string[] args) {
bg.DoWork += bg_DoWork;
bg.ProgressChanged += bg_ProgressChanged;
bg.WorkerReportsProgress = true;
bg.RunWorkerAsync();
Thread.Sleep(10000);
}
static void bg_ProgressChanged(object sender, ProgressChangedEventArgs e) {
Console.WriteLine(e.ProgressPercentage);
Thread.Sleep(100);
Console.WriteLine(e.ProgressPercentage);
}
static void bg_DoWork(object sender, DoWorkEventArgs e) {
for (int i = 0; i < 10; i++) {
bg.ReportProgress(i);
}
}
运行时我得到以下输出:
0 1 1 2 0 3 2 3 5 4 4 6 5 7 7 8 6 9 8 9
据我所知,问题是BackgroundWorker 为每次调用ReportProgress 启动的线程之间的竞争条件。
我如何确保每个 bg_ProgressChanged 的整个主体按照我调用它们的顺序执行? 这就是我想得到的
0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
结果为
Consider the following code:
private static BackgroundWorker bg = new BackgroundWorker();
static void Main(string[] args) {
bg.DoWork += bg_DoWork;
bg.ProgressChanged += bg_ProgressChanged;
bg.WorkerReportsProgress = true;
bg.RunWorkerAsync();
Thread.Sleep(10000);
}
static void bg_ProgressChanged(object sender, ProgressChangedEventArgs e) {
Console.WriteLine(e.ProgressPercentage);
Thread.Sleep(100);
Console.WriteLine(e.ProgressPercentage);
}
static void bg_DoWork(object sender, DoWorkEventArgs e) {
for (int i = 0; i < 10; i++) {
bg.ReportProgress(i);
}
}
When run I get the following output:
0
1
1
2
0
3
2
3
5
4
4
6
5
7
7
8
6
9
8
9
I understand that the issue is a race condition between the threads that BackgroundWorker starts for each call to ReportProgress.
How can I make sure that the whole body of each bg_ProgressChanged gets executed in the order I have called them?
That is I would like to get
0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
as a result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
BackgroundWorker
在调用RunWorkerAsync()
的线程的当前 SynchronizationContext 上引发ProgressChanged
事件。默认 SynchronizationContext 在 ThreadPool 上运行回调,无需任何同步。
如果您在UI应用程序(WPF或WinForms)中使用BackgroundWorker,它将使用该UI平台的SynchronizationContext,它将按顺序执行回调。
BackgroundWorker
raisesProgressChanged
events on the current SynchronizationContext of the thread that calledRunWorkerAsync()
.The default SynchronizationContext runs callbacks on the ThreadPool without any synchronization.
If you use BackgroundWorker in a UI application (WPF or WinForms), it will use that UI platform's SynchronizationContext, which will execute callbacks in order.
不要使用这个解决方案!正如 SLAks 指出的那样,可能会导致僵局。
我似乎偶然发现了一个答案。我按以下方式更改了代码:
现在我得到了我想要的输出:
Do not use this solution!!! May lead to deadlocks as SLaks has pointed it out.
I seem to have stumbled upon an answer. I changed the code the following way:
and now I get the output I want: