简体   繁体   中英

how to use Background Worker in WPF C# in a switch

How can I able to use Background Worker in several times with different Dowork one at a time after one process to another process?

I have this code:

foreach (string item in Processes)
{
    ItemValue = item;
    //Get process code for the process under the selected source
    GetActiveProcess();

    switch (ProcessCode)
    {
        case "Download File":
            GetStartTime();
            UpdateDownloadStartTime();

            if (worker.IsBusy != true)
            {
                worker.DoWork += new DoWorkEventHandler(worker_DoWorkDownload);
                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
                worker.RunWorkerAsync();
            }
            break;

        case "Unzip File":
            GetStartTime();
            UpdateDownloadStartTime();

            if (worker.IsBusy != true)
            {
                worker.DoWork += new DoWorkEventHandler(worker_DoWorkUnzip);
                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
                worker.RunWorkerAsync();
            }
            break;

    }

}

Here is my code for DoWork and RunworkerCompleted

private void worker_DoWorkDownload(object sender, DoWorkEventArgs e)
{
    DownloadFile();
}
private void worker_DoWorkUnzip(object sender, DoWorkEventArgs e)
{
    ExtractFile();
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    GetEndTime();
    GetDuration();
    UpdateDownloadEndTime();
}

As for my code, it continues performing the Dowork at the same time. What I'm wanna do is to perform Backgroundworker for every item in my Process.

How can I do that?

尝试将开关移至DoWork处理程序,并在if块中调用DownloadFile或其他文件,而不是订阅和启动backgroundworker。

Why not switch to async/await instead?

foreach (string item in Processes)
{
  ItemValue = item;
  //Get process code for the process under the selected source
  GetActiveProcess();

  GetStartTime();
  UpdateDownloadStartTime();

  switch (ProcessCode)
  {
    case "Download File":
      await Task.Run(DownloadFile);
      break;

    case "Unzip File":
      await Task.Run(ExtractFile);
      break;
  }

  GetEndTime();
  GetDuration();
  UpdateDownloadEndTime();
}

If you are attempting to run these background workers in a non-concurrent manner, you need to wait until the first background do work function is finished before you can start the next do work function.

This means that you need to remove items off your Processes list differently. Remove the first item as you have, setup your worker and run the doWork, but then each subsequent item should be removed and the worker should be setup in the completed handler (worker_RunWorkerCompleted)

Also, you keep adding do work event handlers to your worker, without ever removing them. And, you only need to add the worker_RunWorkerCompleted handler to the worker once - don't do it for every item you are processing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM