[英]Background Worker with progress bar
I am trying to get a ProgressBar
with the progress of a dataset being converted to Excel using the BackgroundWorker
. 我试图获得一个
ProgressBar
,其中包含使用BackgroundWorker
转换为Excel的数据集的进度。 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. 问题在于工作是在不同于
ProgressBar
的其他类中完成的,而我很难从我的循环中调用worker.ReportProgress(...)
。 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. 如果这是一件容易的事,我感到很抱歉,但是我是C#的新手,并且整天都在尝试这种方法,但似乎做得不好。 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;
}
// ...
}
}
You'll need to pass your worker
to the Get_AccountsToBeReceipted
method - it can then call BackgroundWorker.ReportProgress
: 您需要将您的
worker
传递给Get_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(...);
}
Alternatively, you could make GenBulkReceiptsBLL
have its own Progress
event, and subscribe to that from GenBulkReceipts
- but that would be more complicated. 或者,您可以使
GenBulkReceiptsBLL
具有其自己的Progress
事件,并从GenBulkReceipts
订阅该GenBulkReceipts
-但这会更加复杂。
Your class GenBulkReceiptsBLL
needs some reference to the BackgroundWorker
instance. 您的
GenBulkReceiptsBLL
类需要对BackgroundWorker
实例的一些引用。 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
. 例如,由于
GenBulkReceipts
是实例化类GenBulkReceiptsBLL
,然后在构造函数GenBulkReceiptsBLL
,你可以通过实例BackgroundWorker
当前正在使用中GenBulkReceipts
。 This would allow for you to call ReportProcess(...)
directly. 这将允许您直接调用
ReportProcess(...)
。 Alternately, you could pass the reference directly into the Get_AccountsToBeReceipted(...)
method. 或者,您可以将引用直接传递到
Get_AccountsToBeReceipted(...)
方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.