1

我尝试模拟一个运行时间较长的过程。在我的应用程序中,我使用背景并动态添加进度条。当我点击开始按钮时,一个进程开始并在面板上添加了一个进度条。如果我只单击一次,那么一切正常,但如果我单击两次或更多,则添加该数量的进度条,但最后一个进度条进度增加,最后其他进度条进度突然增加。

我希望添加任何数量的进度条都应该同时处理,而不是最后一个和最后一个。一切都应该像许多进程在自己的上下文中同时运行一样一起运行。这是我的代码,我希望如果有人看一下,他们就能理解缺陷在哪里。请帮我修复最后添加的进度条进度增加的缺陷。如果可能,运行我的代码并查看问题。

public partial class Form2 : Form
{
    MyBackgroundWorker backgroundWorker1 = null;
    Dictionary<string, string> dct = new Dictionary<string, string>();
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        backgroundWorker1 = new MyBackgroundWorker();
        backgroundWorker1.WorkerReportsProgress = true;   
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        backgroundWorker1.RunWorkerAsync(txtNumber.Text);

    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        string strID = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.ToString("hhmmss")+DateTime.Now.Millisecond.ToString();
        MyBackgroundWorker tmpBg = (sender as MyBackgroundWorker);
        tmpBg.Name = "bg_" + strID;

        ProgressBar pb = new ProgressBar();
        pb.Minimum = 0;
        pb.Maximum = 100;
        pb.Name = "pb_" + strID;
        pb.Width = txtNumber.Width;
        this.BeginInvoke((MethodInvoker)delegate
        {
            flowLayoutPanel1.Controls.Add(pb);
        });

        dct.Add(tmpBg.Name, pb.Name);

        int input = int.Parse(e.Argument.ToString());
        for (int i = 1; i <= input; i++)
        {
            Thread.Sleep(2000);
            backgroundWorker1.ReportProgress(i * 10);
            if (backgroundWorker1.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
        }
    }

    // This event handler updates the progress bar. 
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        MyBackgroundWorker tmpBg = (sender as MyBackgroundWorker);
        string strName = tmpBg.Name;

        if (!string.IsNullOrEmpty(strName))
        {
            strName = strName.Substring(3);
            ProgressBar pb = (ProgressBar)this.flowLayoutPanel1.Controls.Find(("pb_" + strName), true)[0];
            pb.Value = e.ProgressPercentage;
        }
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MyBackgroundWorker tmpBg = (sender as MyBackgroundWorker);
        string strName = tmpBg.Name;
        strName = strName.Substring(3);
        ProgressBar pb = (ProgressBar)this.flowLayoutPanel1.Controls.Find(("pb_" + strName), true)[0];
        pb.Value = 100;
        //MessageBox.Show("Done");
    }

}

    public class MyBackgroundWorker : System.ComponentModel.BackgroundWorker
        {
            public MyBackgroundWorker()
            {
            }

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

            public string Name { get; set; }
        }

更新部分

 private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MyBackgroundWorker tmpBg = (sender as MyBackgroundWorker);
            string strName = tmpBg.Name;
            strName = strName.Substring(3);
            ProgressBar pb = (ProgressBar)this.flowLayoutPanel1.Controls.Find(("pb_" + strName), true)[0];
            //pb.Value = 100;
            //MessageBox.Show("Done");
            flowLayoutPanel1.Controls.Remove(pb);
        }
4

1 回答 1

0

我想我让它按照你想要的方式工作。

  1. 在 ProgressChanged 中,我以另一种方式获取进度条。

  2. 在 DoWork 中,我以百分比的形式报告进度,并来自当前的 backgroundWorker。

  3. 我删除Thread.Sleep是因为我认为它会导致问题,添加Console.WriteLineDoWork减慢程序(否则它比ProgressBar渲染更快)

    public partial class Form2 : Form
    {
        MyBackgroundWorker backgroundWorker1 = null;
        Dictionary<string, string> dct = new Dictionary<string, string>();
        public Form2()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
    
        }
    
        private void btnStart_Click(object sender, EventArgs e)
        {
            backgroundWorker1 = new MyBackgroundWorker();
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
            backgroundWorker1.RunWorkerAsync(int.Parse(txtNumber.Text));
    
        }
    
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            string strID = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.ToString("hhmmss") + DateTime.Now.Millisecond.ToString();
            MyBackgroundWorker tmpBg = (sender as MyBackgroundWorker);
            tmpBg.Name = "bg_" + strID;
    
            ProgressBar pb = new ProgressBar();
            pb.Minimum = 0;
            pb.Maximum = 100;
            pb.Name = "pb_" + strID;
            pb.Width = txtNumber.Width;
            this.BeginInvoke((MethodInvoker)delegate
            {
                flowLayoutPanel1.Controls.Add(pb);
            });
    
            dct.Add(tmpBg.Name, pb.Name);
    
            int input = (int)e.Argument;
            for (int i = 1; i <= input; i++)
            {
                Console.WriteLine(tmpBg.Name + ": " + (i * 100 / input));   //for testing (this line slows the program down a lot)
                tmpBg.ReportProgress(i * 100 / input);
                if (tmpBg.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }
    
        // This event handler updates the progress bar. 
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            MyBackgroundWorker tmpBg = (sender as MyBackgroundWorker);
            string strName = tmpBg.Name.Substring(3);
            ProgressBar pb;
    
            if (!string.IsNullOrEmpty(strName))
            {
                foreach (Control c in flowLayoutPanel1.Controls)
                {
                    if (c.Name == ("pb_" + strName))
                    {
                        pb = (ProgressBar)c;
                        pb.Value = Math.Min(100,e.ProgressPercentage);
                    }
                }
            }
        }
    
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MyBackgroundWorker tmpBg = (sender as MyBackgroundWorker);
            string strName = tmpBg.Name;
            strName = strName.Substring(3);
            ProgressBar pb = (ProgressBar)this.flowLayoutPanel1.Controls.Find(("pb_" + strName), true)[0];
            pb.Value = 100;
            //MessageBox.Show("Done");
        }
    
    }
    
    public class MyBackgroundWorker : System.ComponentModel.BackgroundWorker
    {
        public MyBackgroundWorker()
        {
        }
    
        public MyBackgroundWorker(string name)
        {
            Name = name;
        }
    
        public string Name { get; set; }
    }
    
于 2013-03-28T17:09:05.637 回答