如何让BackgroundWorker ProgressChanged事件按顺序执行?

发布于 2024-12-14 01:04:37 字数 959 浏览 1 评论 0原文

考虑以下代码:

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 技术交流群。

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

发布评论

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

评论(2

云裳 2024-12-21 01:04:37

BackgroundWorker 在调用 RunWorkerAsync() 的线程的当前 SynchronizationContext 上引发 ProgressChanged 事件。

默认 SynchronizationContext 在 ThreadPool 上运行回调,无需任何同步。

如果您在UI应用程序(WPF或WinForms)中使用BackgroundWorker,它将使用该UI平台的SynchronizationContext,它将按顺序执行回调。

BackgroundWorker raises ProgressChanged events on the current SynchronizationContext of the thread that called RunWorkerAsync().

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.

难如初 2024-12-21 01:04:37

不要使用这个解决方案!正如 SLAks 指出的那样,可能会导致僵局。

我似乎偶然发现了一个答案。我按以下方式更改了代码:

   [MethodImpl(MethodImplOptions.Synchronized)]
   static void bg_ProgressChanged(object sender, ProgressChangedEventArgs e) {
     Console.WriteLine(e.ProgressPercentage);
     Thread.Sleep(100);
     Console.WriteLine(e.ProgressPercentage);
   }

现在我得到了我想要的输出:

0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9

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:

   [MethodImpl(MethodImplOptions.Synchronized)]
   static void bg_ProgressChanged(object sender, ProgressChangedEventArgs e) {
     Console.WriteLine(e.ProgressPercentage);
     Thread.Sleep(100);
     Console.WriteLine(e.ProgressPercentage);
   }

and now I get the output I want:

0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9

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