我的问题是关于处理多个背景工作者和多个进度条

时间:2013-03-28 10:29:34

标签: c# winforms progress-bar backgroundworker

我的主要问题是当我在flowLayoutPanel1上为每个请求动态添加进度条时,如何从backgroundWorker1_ProgressChanged事件增加右进度条值

当用户点击开始按钮然后我将调用后台工作者的RunWorkerAsync功能....这很好。在这里,我正在编写示例代码并尝试显示我的问题。

我的表单有一个文本框,一个按钮和一个带有FlowDirection topdown的flowLayoutPanel1。

当用户在文本框中输入任何URL并单击开始按钮时,我将使用BackGroundWorker开始文件下载,并在flowLayoutPanel1上动态添加一个进度条。一次可以下载10个文件。所以当用户一个接一个地输入十个网址并点击提交按钮后,将在后台下载十个文件。

我的问题是如何从backgroundWorker1_ProgressChanged事件正确增加10个进度条进度另一个问题是当任何文件下载完成后,将从面板中删除已创建它的进度条

这是我的完整代码。请看看,告诉我我需要做什么来完成我的任务。如果有人看到我的代码中有问题可能会出现死锁,那么请指导我如何避免死锁。这是我的代码

public partial class Form1 : Form
    {
        static int pbCounter = 1;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            ProgressBar pb = new ProgressBar();
            if (pbCounter <= 10)
            {
                pb.Width = txtUrl.Width;
                flowLayoutPanel1.Controls.Add(pb);
                pbCounter++;

                System.ComponentModel.BackgroundWorker backgroundWorker1 = new System.ComponentModel.BackgroundWorker();

                backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
                backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
                backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            // Get the BackgroundWorker that raised this event.
            System.ComponentModel.BackgroundWorker worker = sender as System.ComponentModel.BackgroundWorker;

            // Assign the result of the computation
            // to the Result property of the DoWorkEventArgs
            // object. This is will be available to the  
            // RunWorkerCompleted eventhandler.
            //#e.Result = ComputeFibonacci((int)e.Argument, worker, e);
        }

        // This event handler deals with the results of the
        // background operation.
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // First, handle the case where an exception was thrown.
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
                //# "Canceled";
            }
            else
            {
                pbCounter--;
                // Finally, handle the case where the operation  
                // succeeded.
                //#resultLabel.Text = e.Result.ToString();
            }
        }

        // This event handler updates the progress bar.
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.progressBar1.Value = e.ProgressPercentage;
        }

    }

我的更新部分

 public partial class Form1 : Form
    {
        static int pbCounter = 1;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            ProgressBar pb = new ProgressBar();
            if (pbCounter <= 10)
            {
                //pb.Step = 10;
                pb.Minimum = 0;
                //pb.Maximum = 100;
                pb.Width = txtUrl.Width;
                flowLayoutPanel1.Controls.Add(pb);
                pbCounter++;

                MyBackgroundWorker backgroundWorker1 = new MyBackgroundWorker();
                backgroundWorker1.pbProgress = pb;
                backgroundWorker1.WorkerReportsProgress = true;   
                backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
                backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
                backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

                backgroundWorker1.RunWorkerAsync(txtUrl.Text);
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int input = int.Parse(e.Argument.ToString());
            for (int i = 1; i <= input; i++)
            {
                Thread.Sleep(2000);
                 (sender as MyBackgroundWorker).ReportProgress(i * 10);
                 if ((sender as MyBackgroundWorker).CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }

        // This event handler deals with the results of the 
        // background operation. 
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // First, handle the case where an exception was thrown. 
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
                //# "Canceled";
            }
            else
            {
                ProgressBar pb = (sender as MyBackgroundWorker).pbProgress;
                if (pb != null)
                {
                    //pb.Value = 100;
                    //pb.Update();

                    while (pb.Value <= pb.Maximum)
                    {
                        //Thread.Sleep(1000);
                        flowLayoutPanel1.Controls.Remove(pb);
                        break;
                    }

                }
                // Finally, handle the case where the operation  
                // succeeded.
            }
        }

        // This event handler updates the progress bar. 
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            ProgressBar pb = (sender as MyBackgroundWorker).pbProgress;
            pb.Refresh();

            //if (e.ProgressPercentage < pb.Maximum)
            //    pb.Value = e.ProgressPercentage + 10;

            pb.Value = e.ProgressPercentage ;
            Application.DoEvents();

        }

    }

public class MyBackgroundWorker : System.ComponentModel.BackgroundWorker
    {
        public ProgressBar pbProgress = null;
        public MyBackgroundWorker()
        {
        }

        public MyBackgroundWorker(string name)
        {
            Name = name;
        }

        public string Name { get; set; }
    }

2 个答案:

答案 0 :(得分:1)

这是一种方法 -

创建自己继承自BackgroundWorker的类,添加ProgressBar类型的公共变量。

每次动态添加BackgroundWorker和Progressbar时,请将progressbar对象传递给您的类。

您的派生类 -

public class MyBackgroundWorker : BackgroundWorker
{
    public ProgressBar pbProgress = null;

    public void BackgroundWorker()
    {
    }
}

按钮事件代码 -

private void btnStart_Click(object sender, EventArgs e)
{
    ProgressBar pb = new ProgressBar();
    if (pbCounter <= 10)
    {
        pb.Width = txtUrl.Width;
        flowLayoutPanel1.Controls.Add(pb);
        pbCounter++;

        MyBackgroundWorker backgroundWorker1 = new MyBackgroundWorker();
        backgroundWorker1.pbProgress = pb;

        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

        backgroundWorker1.RunWorkerAsync();
    }
}

进度改变事件 -

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        (sender as MyBackgroundWorker).pbProgress.Value = e.ProgressPercentage;
    }

答案 1 :(得分:1)

首先声明一个全局字典和一个GetInstance方法来访问表单实例

public partial class Form1 : Form
{
Dictionary<String, ProgressBar> progressBars = new Dictionary<String, ProgressBar>();

static Form1 _form1 = null;

static int pbCounter = 1;

public Form1()
{
    InitializeComponent();
    _form1 = this;
}

public static Form1 GetInstance() {
    return _form1;
}

然后你得到每个网址,你必须为每个网址创建pb,只需将它们添加到这个字典中

progressBars.Add("file1", pb1);
progressBars.Add("file2", pb2);
progressBars.Add("file3", pb3);
progressBars.Add("file4", pb4);

在form.cs中创建一个函数,您可以在其中传递进度条,然后您可以手动设置它的值。

public void ProgessReport(ProgressBar pb, int value) 
        {
            if (pb.InvokeRequired) 
            {
                pb.Invoke(new MethodInvoker(delegate { ProgessReport(pb, value); }));
            } else 
            {
                pb.Value = value;
            }
        }

现在从您下载文件的地方,您只需要致电

Form1.GetInstance().ProgessReport(Form1.GetInstance().progressBars["file1"], 10);
Form1.GetInstance().ProgessReport(Form1.GetInstance().progressBars["file1"], 20);
Form1.GetInstance().ProgessReport(Form1.GetInstance().progressBars["file1"], 100);

and when your second file downloads then

Form1.GetInstance().ProgessReport(Form1.GetInstance().progressBars["file2"], 10);
Form1.GetInstance().ProgessReport(Form1.GetInstance().progressBars["file2"], 20);
Form1.GetInstance().ProgessReport(Form1.GetInstance().progressBars["file2"], 100);
像这样......