Reputation: 25
I am trying to get a ProgressBar
with the progress of a dataset being converted to Excel using the BackgroundWorker
. The problem is that the work is being done in a different class than the ProgressBar
and I am having difficulty calling worker.ReportProgress(...)
from within my loop. I am sorry if this is a easy thing but I am new to C# and have been trying this the whole day and just can't seem to get it right. Your help would be HIGHLY appreciated.
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;
}
// ...
}
}
Upvotes: 0
Views: 1341
Reputation: 8623
Your class GenBulkReceiptsBLL
needs some reference to the BackgroundWorker
instance. You can achieve this in a variety of ways. One such suggestion would be to pass the reference to the class when instantiating it.
For example, since GenBulkReceipts
is the class that instantiates GenBulkReceiptsBLL
, then in the constructor for GenBulkReceiptsBLL
, you could pass the instance of the BackgroundWorker
that is currently being used in GenBulkReceipts
. This would allow for you to call ReportProcess(...)
directly. Alternately, you could pass the reference directly into the Get_AccountsToBeReceipted(...)
method.
Upvotes: 0
Reputation: 1503839
You'll need to pass your worker
to the Get_AccountsToBeReceipted
method - it can then call 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(...);
}
Alternatively, you could make GenBulkReceiptsBLL
have its own Progress
event, and subscribe to that from GenBulkReceipts
- but that would be more complicated.
Upvotes: 1