How to wait correctly until BackgroundWorker completes?

前端 未结 10 1208
既然无缘
既然无缘 2020-12-02 11:19

Observe the following piece of code:

var handler = GetTheRightHandler();
var bw = new BackgroundWorker();
bw.RunWorkerCompleted += OnAsyncOperationCompleted;         


        
10条回答
  •  生来不讨喜
    2020-12-02 11:45

    To wait for a background worker thread (single or multiple) do the following:

    1. Create a List of Background workers you have programatically created:

      private IList m_WorkersWithData = new List();
      
    2. Add the background worker in the list:

      BackgroundWorker worker = new BackgroundWorker();
      worker.DoWork += new DoWorkEventHandler(worker_DoWork);
      worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
      worker.WorkerReportsProgress = true;
      m_WorkersWithData.Add(worker);
      worker.RunWorkerAsync();
      
    3. Use the following function to wait for all workers in the List:

      private void CheckAllThreadsHaveFinishedWorking()
      {
          bool hasAllThreadsFinished = false;
          while (!hasAllThreadsFinished)
          {
              hasAllThreadsFinished = (from worker in m_WorkersWithData
                                       where worker.IsBusy
                                       select worker).ToList().Count == 0;
              Application.DoEvents(); //This call is very important if you want to have a progress bar and want to update it
                                      //from the Progress event of the background worker.
              Thread.Sleep(1000);     //This call waits if the loop continues making sure that the CPU time gets freed before
                                      //re-checking.
          }
          m_WorkersWithData.Clear();  //After the loop exits clear the list of all background workers to release memory.
                                      //On the contrary you can also dispose your background workers.
      }
      

提交回复
热议问题