簡體   English   中英

后台工具與進度欄

[英]Background Worker with progress bar

我試圖獲得一個ProgressBar ,其中包含使用BackgroundWorker轉換為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;
        }

        // ...
    }
}

您需要將您的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訂閱該GenBulkReceipts -但這會更加復雜。

您的GenBulkReceiptsBLL類需要對BackgroundWorker實例的一些引用。 您可以通過多種方式來實現。 一種建議是在實例化類時將引用傳遞給該類。

例如,由於GenBulkReceipts是實例化類GenBulkReceiptsBLL ,然后在構造函數GenBulkReceiptsBLL ,你可以通過實例BackgroundWorker當前正在使用中GenBulkReceipts 這將允許您直接調用ReportProcess(...) 或者,您可以將引用直接傳遞到Get_AccountsToBeReceipted(...)方法中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM