带有进度条的后台工作者

本文关键字:后台 工作者 | 更新日期: 2023-09-27 18:21:40

我正在尝试使用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;
        }
        // ...
    }
}

带有进度条的后台工作者

您需要将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(...);
}

或者,您可以让GenBulkReceiptsBLL拥有自己的Progress事件,并从GenBulkReceipts订阅该事件,但这会更复杂。

您的类GenBulkReceiptsBLL需要对BackgroundWorker实例进行一些引用。你可以通过多种方式来实现这一点。一个这样的建议是在实例化类时将引用传递给它

例如,由于GenBulkReceipts是实例化GenBulkReceiptsBLL的类,因此在GenBulkReceiptsBLL的构造函数中,可以传递当前在GenBulkReceipts中使用的BackgroundWorker的实例。这将允许您直接致电ReportProcess(...)。或者,您可以将引用直接传递到Get_AccountsToBeReceipted(...)方法中。