有进展酒吧的背景工作者

时间:2012-01-27 13:25:06

标签: c# winforms progress-bar backgroundworker

我正在尝试使用ProgressBarBackgroundWorker数据集的进度转换为Excel。问题是这项工作是在与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;
        }

        // ...
    }
}

2 个答案:

答案 0 :(得分:1)

您需要将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订阅 - 但这会更复杂。

答案 1 :(得分:0)

您的班级GenBulkReceiptsBLL需要对BackgroundWorker个实例进行一些引用。您可以通过各种方式实现这一目标。其中一个建议就是在实例化时将引用传递给类。

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