本文介绍了检查多个BackgroundWorker的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用循环创建了多个BackgroundWorkers:

So I create multiple BackgroundWorkers using loop:

  foreach (var item in list)
  {
    worker = new BackgroundWorker();
    worker.DoWork += worker_DoWork;
    worker.RunWorkerAsync(item);
  }

现在我不知道该如何检查所有这些工人是否都完成了工作?

Now I just can't figure how do I check that all those workers finished their job ?

谢谢.

推荐答案

您可以保留一个活动的工作人员列表,并在完成后让每个工作人员将自己删除:

You may keep a list of active workers and let each worker remove itself when finished:

List<BackgroundWorker> workers = new List<BackgroundWorker>();

...

  foreach (var item in list)
  {
    worker = new BackgroundWorker();
    worker.DoWork += worker_DoWork;
    workers.Add(worker);
    worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
    {
        BackgroundWorker worker = (BackgroundWorker)s;
        workers.Remove(worker);
    }
    worker.RunWorkerAsync(item);
  }

...

public bool IsWorkDone
{
    get { return workers.Count == 0; }
}

如果您不希望合并IsWorkDone,则可以在workers.Remove(worker)之后引发一个事件,如果列表为空...

If you don't want to pool IsWorkDone, you could raise an event after workers.Remove(worker) if list is empty...

这篇关于检查多个BackgroundWorker的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 12:37