如何在按钮中运行多个后台进程

时间:2011-05-29 03:27:10

标签: c# backgroundworker

我遇到了问题。我是c#的新手。在此代码中,当我单击button2时,它将仅运行bgutures.RunWorkerAsync();。但我需要对其进行修改,如果我点击button2checkedListBox1.allitems包含的所有项目都会在bgworker中逐个下载。任何人都可以请帮助。 我的代码是:

   private void button2_Click(object sender, EventArgs e)
    {
        button2.Enabled = false;
        string DayAtoZ = " ";
        string DayDead = " ";
        string Dayutures = " ";
        string Daysify = " ";
        string Dayfx = " ";

        if (checkedListBox1.SelectedItem == "")
        {

        }
        else if (checkedListBox1.SelectedItem == Dayutures)
        {


        }
        else if (checkedListBox1.SelectedItem == DayAtoZ)
        {

        }

        else if (checkedListBox1.SelectedItem == Daysify)
        {

        }
        else if (checkedListBox1.SelectedItem == DayDead)
        {

        }
        else if (checkedListBox1.SelectedItem == Dayfx)
        {

        }

        {
            bgutures.RunWorkerAsync();
        }
        {
            bgDead.RunWorkerAsync();
        }


        {
            bgsify.RunWorkerAsync();
        }



        {
            bgAtoZ.RunWorkerAsync();
        }
        {

        }

    }

1 个答案:

答案 0 :(得分:1)

而不是一个BackgoundWorker使用尽可能多的人。

    if (checkedListBox1.SelectedItem == "")
    {
       var worker = new BackgroundWorker();
       .. do setup
       worker.RunWorkerAsync();
    }
    else if (checkedListBox1.SelectedItem == Dayutures)
    {
       var worker = new BackgroundWorker();
       .. do setup
       worker.RunWorkerAsync();
    }