我的程序里面是用的强类型,数据是从一个数据库提交到另一个数据库,当然,是循环,然后客户要求显示进度,我开始至是使用 this.lblSubmitPercent.Text = num + "/" + count; 这个代码是在 btn 的点击事件下的 ,它只有在全部数据提交完全后才显示出来,后来我用了 prigressbar 进度条控件,但是问题出来了:那个循环很快便跑完了,进度条也满了,但是后面的 update方法那里 便会卡很久,没有达到 进度条跑满,就弹出 “数据提交成功”的提示的要求,进度条满了后会有很长一段时间,就像处于 死机状态一样(更新数据库数据中)。大家看看怎么解决
            //代码片段一
            this.lblSubmitPercent.Visible = true;
            progressBar1.Visible = true;
            progressBar1.Minimum = 0;
            progressBar1.Maximum = count;
            progressBar1.BackColor = Color.Green;            //代码片段2
            for(……)
            {    
                 ……
                 progressBar1.Value++;
                //Application.DoEvents();
                this.lblSubmitPercent.Text = num + "/" + count; // 显示如:4/10,count为总数,num为进度
                this.lblSubmitPercent.Refresh();
            }            //代码片段三,就是下面的 update时会卡很长一段时间
            UpdateOnLineData(itemids, GetTitemId());//更新在線服務器庫存             
            int val = OnLineTorder.Update(ONmds.TOrder);
            OnLineTorderprintout1.Update(ONmds.TOrderPrintout1);
            OnLineTOrderItemQuantity.Update(ONmds.TOrderItemQuantity);
            OnLineTItemStockHistory.Update(ONmds.TItemStockHistory);
            UpdateCode();//更新在線流水號
            DelData();//刪除已經提交的數據
            mySqlTransaction.Commit();

解决方案 »

  1.   


                this.lblSubmitPercent.Visible = true;
                
               progressBar1.Visible = true;
                progressBar1.Minimum = 0;
                progressBar1.Maximum = count;
                progressBar1.BackColor = Color.Green;            //代码片段2
                for(……)
                {    
                     ……
                     progressBar1.Value++;
                    //Application.DoEvents();
                    this.lblSubmitPercent.Text = num + "/" + count; // 显示如:4/10,count为总数,num为进度
                    this.lblSubmitPercent.Refresh();
                }
     fresh();
                //代码片段三,就是下面的 update时会卡很长一段时间
                UpdateOnLineData(itemids, GetTitemId());//更新在線服務器庫存             
     fresh();
     int val = OnLineTorder.Update(ONmds.TOrder);
     fresh();         
      OnLineTorderprintout1.Update(ONmds.TOrderPrintout1);
     fresh();      
        OnLineTOrderItemQuantity.Update(ONmds.TOrderItemQuantity);
     fresh();     
          OnLineTItemStockHistory.Update(ONmds.TItemStockHistory);
    fresh();        
      UpdateCode();//更新在線流水號
    fresh();
         DelData();//刪除已經提交的數據
      mySqlTransaction.Commit();
    void {
               this.lblSubmitPercent.Text = ++num + "/" + count; // 显示如:4/10,count为总数,num为进度
                    this.lblSubmitPercent.Refresh();
    }這樣如何?每走一步更新一下
      

  2.   

    要么使用线程,要么使用BackgroundWorker(这个简单一些),BackgroundWorker的用法,
    http://szitr.com/bbs/thread-320-1-1.html
      

  3.   


    但是 我的那个进度条我放在 循环里面,它是 全部都跑满了,才运行下面的 update方法的,强类型就是这样的
      

  4.   


                    progressBar1.Maximum = 7;
                    this.label10.Visible = false;
                    this.label11.Visible = false;
                    this.lblSubmitPercent.Visible = false;
                    this.label12.Visible = true;
                    this.label12.Text = "數據更新中,請稍候……";                int[] itemids = new int[itemidArr.Count];
                    for (int i = 0; i < itemidArr.Count; i++)
                    {
                        itemids[i] = (int)itemidArr[i];
                    }                UpdateOnLineData(itemids, GetTitemId());//更新在線服務器庫存    
                    progressBar1.Value++;
                    Application.DoEvents();                int val = OnLineTorder.Update(ONmds.TOrder);
                    progressBar1.Value++;
                    Application.DoEvents();                OnLineTorderprintout1.Update(ONmds.TOrderPrintout1);
                    progressBar1.Value++;
                    Application.DoEvents();                OnLineTOrderItemQuantity.Update(ONmds.TOrderItemQuantity);
                    progressBar1.Value++;
                    Application.DoEvents();                OnLineTItemStockHistory.Update(ONmds.TItemStockHistory);
                    progressBar1.Value++;
                    Application.DoEvents();                UpdateCode();//更新在線流水號
                    progressBar1.Value++;
                    Application.DoEvents();                DelData();//刪除已經提交的數據
                    progressBar1.Value++;
                    Application.DoEvents();                mySqlTransaction.Commit();
    我这样才达到要求了,晕死,循环里面我取消了,下面的是更新一张表,进度条就跑一次,哎……