问题进度条运行直到我的进程完成(在数据库中加载文件)

时间:2014-03-16 23:56:53

标签: c# excel visual-studio-2012 progress-bar backgroundworker

我希望在点击按钮SUBMIT时将数据导入数据库的那一刻取得进展我想在导入数据的那一刻在线进度的结果而不是在等待导入数据的时刻之后

我尝试多次使用methode但是我在这里得到错误bgw.RunWorkerAsync()

 public ImportAvancement()
            {
                InitializeComponent();
                label11.Text = "";
                label12.Text = "";
            }
            BackgroundWorker bgw = new BackgroundWorker();    
            DataSet dataset;
            private void ImportAvancement_Load(object sender, EventArgs e)
            {
                label2.Visible = false;
                label3.Visible = false;
                label4.Visible = false;
                label5.Visible = false;
                label6.Visible = false;
                label7.Visible = false;
                label8.Visible = false;
                label9.Visible = false;
                label10.Visible = false;

                dataset= Models.Loadcombo.LoadComboEFP();
                comboBox1.DisplayMember = "nom_Etab";
                comboBox1.ValueMember = "nom_Etab";
                comboBox1.DataSource = dataset.Tables["TEFP"];


            }
            OpenFileDialog open = new OpenFileDialog();
            private void button1_Click(object sender, EventArgs e)
            {

                open.ShowDialog();



            }
            int VALueSector = 0;

            int ValueLevel = 0;
            int ValueAffilate = 0;

            public void work()
            {
                bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
                bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
                bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
                bgw.WorkerReportsProgress = true;
                bgw.RunWorkerAsync();
            }
            private void SUBMIT_Click(object sender, EventArgs e)
            {
                TotalValue = 0;

                VALueSector = Controller.MethodeImportation.ImportSector(open.FileName);
                 TotalValue += VALueSector;
                if (VALueSector == 0)
                {

                    label2.Text = "The SECTORS  ALREADY EXIST";
                }
                else
                {
                    label2.Text = ("" + VALueSector.ToString() + " new Sector have been imported");

                }




                ValueLevel = Controller.MethodeImportation.ImportLevel(open.FileName);
                TotalValue += ValueLevel;
                if (ValeurEFPSECTEUR == 0)
                {

                    label4.Text = "The Levels are already Exist";
                }
                else
                {
                    label4.Text = ("" + ValueLevel.ToString() + " new levels Have been imported");

                }


                ValueAffilate = Controller.MethodeImportation.ImportAffilate(open.FileName);
                 TotalValue += ValueAffilate;
                if (ValueAffilate== 0)
                {

                    label5.Text = "The Affilate already Exist";
                }
                else
                {
                    label5.Text = ("" + ValueAffilate.ToString() + " New Affilate Have been imported");

                }


                work();



            }
            void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                progressBarX1.Visible = false;
                label2.Visible = true;
                label3.Visible = true;
                label4.Visible = true;
                label5.Visible = true;
                label6.Visible = true;
                label7.Visible = true;
                label8.Visible = true;
                label9.Visible = true;
                label10.Visible = true;

            }

            void bgw_DoWork(object sender, DoWorkEventArgs e)
            {


               if (totalValue == 0)
               {
                   bgw.ReportProgress(0, 0);
               }
               else
               {
                   for (int i = 0; i <= totalValue; i++) //some number (total)
                   {
                       System.Threading.Thread.Sleep(100);
                       int percents = (i * 100) / totalValue;
                       bgw.ReportProgress(percents, i);


                   }
               }
            }
            void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {

                progressBarX1.Value = e.ProgressPercentage;
                label11.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
                label12.Text = String.Format("Total items transfered: {0}", e.UserState);
            }

因此当我尝试按照我想要的方式制作我的方法时,多次获得动态总值,就像这段代码一样:

private void SUBMIT_Click(object sender, EventArgs e)
            {
                TotalValue = 0;

                VALueSector = Controller.MethodeImportation.ImportSector(open.FileName);
                 TotalValue += VALueSector;
                if (VALueSector == 0)
                {

                    label2.Text = "The SECTORS  ALREADY EXIST";
                }
                else
                {
                    label2.Text = ("" + VALueSector.ToString() + " new Sector have been imported");

                }

                work();


                ValueLevel = Controller.MethodeImportation.ImportLevel(open.FileName);
                TotalValue += ValueLevel;
                if (ValeurEFPSECTEUR == 0)
                {

                    label4.Text = "The Levels are already Exist";
                }
                else
                {
                    label4.Text = ("" + ValueLevel.ToString() + " new levels Have been imported");

                }

    work();
                ValueAffilate = Controller.MethodeImportation.ImportAffilate(open.FileName);
                 TotalValue += ValueAffilate;
                if (ValueAffilate== 0)
                {

                    label5.Text = "The Affilate already Exist";
                }
                else
                {
                    label5.Text = ("" + ValueAffilate.ToString() + " New Affilate Have been imported");

                }


                work();



            }

最后我收到了这个错误:

BackgroundWorker当前正忙,无法同时运行多个任务。

我使用ado.net模式连接所以需要时间我导入数据我想浪费这个时间并且他将在进度条中并且在计算总计当前总数的同时不计算最终总数

我已编辑了我的方法,这样没有错误但是被阻止了

public void work()
        {
            bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
            bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
            bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
            bgw.WorkerReportsProgress = true;
            bgw.RunWorkerAsync();
            while (this.bgw.IsBusy)
            {

                progressBarX1.Increment(1);
                bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
                bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
                bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
                // Keep UI messages moving, so the form remains
                // responsive during the asynchronous operation.
                Application.DoEvents();
            }
        }

0 个答案:

没有答案