2016-11-27 58 views
0

我試圖顯示進度的百分比值與標籤中的後臺工作人員.Im添加Aprox。 25 K行到一個DataTable。當我將Label.Text設置爲e.ProgressPercentage時,它工作正常。但是,當我計算百分比值,它的工人完成後仍然same.Only標籤更新爲100%計算的進度百分比未更新

   progressCount = report.Rows.Count; 
       foreach (DataRow r in report.Rows) 
       { 

         rp.pName = r[1].ToString(); 
         rp.batch = r[2].ToString(); 
         rp.expr = r[3].ToString(); 
         rp.stock = r[5].ToString(); 
         rp.rate = r[6].ToString(); 
         backgroundWorker2.ReportProgress(i, rp); 
         System.Threading.Thread.Sleep(2); 
         if(backgroundWorker2.CancellationPending) 
         { 
          e.Cancel = true; 
          backgroundWorker2.ReportProgress(0); 
         } 
         i++; 

       } 



private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     metroProgressBar1.Minimum = 0; 
     metroProgressBar1.Maximum = progressCount; 
     stock st = new stock(); 
     reportClass rp = (reportClass)e.UserState; 
     if(!backgroundWorker2.CancellationPending) 
     { 
      st.stockReport.Rows.Add(rp.pName, rp.batch, rp.expr, rp.stock, rp.rate); 
      metroProgressBar1.Value = e.ProgressPercentage; 
      int percn = (e.ProgressPercentage/progressCount) * 100; 
      metroLabel4.Text =percn.ToString(); 
     } 
    } 
+0

你可能不註冊事件:backgroundWorker2.ProgressChanged + =新ProgressChangedEventHandler(backgroundWorker2_ProgressChanged); – jdweng

回答

3

你最有可能將一個整數一個整數,這將導致爲零。將您的值先轉換爲double,然後執行計算,然後將結果轉換回int。

變化:

int percn = (e.ProgressPercentage/progressCount) * 100; 

要:

int percn = (int)(((double)e.ProgressPercentage/(double)progressCount) * 100);