2012-01-27 42 views
0

我正在尝试使用BackgroundWorker将数据集的进度转换为Excel,从而获得ProgressBar。问题在于工作是在与ProgressBar不同的课程中完成的,并且我很难在我的循环中调用worker.ReportProgress(...)。我很抱歉,如果这是一件容易的事情,但我是C#的新手,并且一直在尝试这一整天,并且看起来似乎无法做到。高度赞赏您的帮助。具有进度条的背景工作人员

namespace CLT 
{ 
    public partial class GenBulkReceipts : UserControl 
    { 
     private void btnOpen_Click(object sender, EventArgs e) 
     { 
      Cursor.Current = Cursors.WaitCursor; 
      try 
      { 
       OpenFile(); 
      } 

      Cursor.Current = Cursors.Default; 
     } 
     private void OpenFile() 

     { 
      if (dsEx1.Tables[0].Rows.Count > 0) 
      { 
        backgroundWorker1.RunWorkerAsync(dsEx1); 
      } 
     } 

     public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
     { 
      BackgroundWorker worker = sender as BackgroundWorker; 
      DataSet ImportDataSet = e.Argument as DataSet; 
      AccountsToBeImported = new BLLService().Get_AccountsToBeReceipted(ImportDataSet); 
     } 

     public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
     { 
      this.progressBar1.Value = e.ProgressPercentage; 
     } 

     // ... 
    } 
} 

namespace BLL 
{ 
    class GenBulkReceiptsBLL 
    { 
     public DataSet Get_AccountsToBeReceipted(DataSet dsImport) 
     { 
      DataSet dsReturn = AccountsDAL.QGenReceiptAccounts(0,0,"");//Kry Skoon DataSet wat ge-populate moet word 

      CLT.GenBulkReceipts pb = new CLT.GenBulkReceipts(); 
      int TotalRows = dsImport.Tables[0].Rows.Count; 
      //pb.LoadProgressBar(TotalRows); 
      int calc = 1; 
      int ProgressPercentage; 

      foreach (DataRow dr in dsImport.Tables[0].Rows) 
      { 
       ProgressPercentage = (calc/TotalRows) * 100; 

       //This is the problem as I need to let my Progressbar progress here but I am not sure how 
       //pb.worker.ReportProgress(ProgressPercentage); 
      } 

      return dsReturn; 
     } 

     // ... 
    } 
} 
+0

位于'btnOpen_Click(...)'方法中'try-catch'的catch子句在哪里? – 2012-01-27 13:34:15

+0

请确保您接受答案,如果它导致您的问题的解决;并投票你觉得有用的任何东西。 – 2012-01-27 20:06:35

+0

它在那里,我只是没有包括它在这里,使代码示例分类器 – user1171437 2012-01-30 07:20:38

回答

0

您的类GenBulkReceiptsBLL需要一些参考BackgroundWorker实例。您可以通过多种方式实现此目的。一个这样的建议是在实例化时将参考传递给类。

例如,由于GenBulkReceipts是实例GenBulkReceiptsBLL,然后在构造函数GenBulkReceiptsBLL类,你可以通过当前正在GenBulkReceipts使用的BackgroundWorker的实例。这将允许您直接拨打ReportProcess(...)。或者,您可以直接将参考传递给Get_AccountsToBeReceipted(...)方法。

+0

当我直接将它传递给公共DataSet Get_AccountsToBeReceipted(DataSet dsImport,BackgroundWorker worker)时,GenBulkReceiptsBLL不拾取Backgroundworker。另外当我将BackgroundWorker的实例传递给GenBulkReceiptsBLL构造函数时。如果可能的话,我会很感激一个例子。 – user1171437 2012-01-27 13:54:49

+0

对不起,只需要使用参考 – user1171437 2012-01-27 14:05:28

+0

@ user1171437我认为你现在工作了吗? – 2012-01-27 14:13:01

1

你需要通过你的workerGet_AccountsToBeReceipted方法 - 那么它可以调用BackgroundWorker.ReportProgress

// In GenBulkReceipts 
public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
{ 
    BackgroundWorker worker = sender as BackgroundWorker; 
    DataSet importDataSet = e.Argument as DataSet; 
    AccountsToBeImported = 
     new BLLService().Get_AccountsToBeReceipted(importDataSet, worker); 
} 

// In GenBulkReceiptsBLL 
public DataSet Get_AccountsToBeReceipted(DataSet dsImport, 
             BackgroundWorker worker) 
{ 
    ... 
    worker.ReportProgress(...); 
} 

或者,你可以做GenBulkReceiptsBLL有自己的Progress事件,并从订阅的GenBulkReceipts - 但那会更复杂。

+0

在上面的例子公共DataSet Get_AccountsToBeReceipted(数据集dsImport,BackgroundWorker工人)Backgroundworker是不是拾取相同的DataSet – user1171437 2012-01-27 13:57:27

+0

@ user1171437:目前还不清楚你的意思是“没有拿起”。 – 2012-01-27 13:59:56

+0

对不起,只需要使用参考 – user1171437 2012-01-27 14:02:27