【问题标题】:Background Worker with progress bar带进度条的后台工作者
【发布时间】:2012-01-27 21:33:54
【问题描述】:

我正在尝试获取一个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;
        }

        // ...
    }
}

【问题讨论】:

  • try-catchcatch 子句在btnOpen_Click(...) 方法中的位置在哪里?
  • 如果您的问题得到解决,请确保您接受答案;并为您认为有用的任何内容投票。
  • 它在那里我只是没有在这里包含它来制作代码示例排序器

标签: c# winforms progress-bar backgroundworker


【解决方案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 订阅该事件 - 但这会更复杂。

【讨论】:

  • 如上例中 public DataSet Get_AccountsToBeReceipted(DataSet dsImport, BackgroundWorker worker) Backgroundworker 与 DataSet 不同
  • @user1171437:不清楚您所说的“未领取”是什么意思。
  • 抱歉漫长的一天只需要使用参考
【解决方案2】:

您的类GenBulkReceiptsBLL 需要一些对BackgroundWorker 实例的引用。您可以通过多种方式实现这一目标。一种这样的建议是在实例化类时传递对类的引用。

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

【讨论】:

  • 当我将 GenBulkReceiptsBLL 直接传递给公共 DataSet Get_AccountsToBeReceipted(DataSet dsImport, BackgroundWorker worker) 时,它不会获取 Backgroundworker。此外,当我将 BackgroundWorker 的实例传递给 GenBulkReceiptsBLL 构造函数时。如果可能的话,我会很感激一个例子。
  • 抱歉漫长的一天只需要使用参考
  • 尚未进度条似乎还没有进展
  • @user1171437 如果您发布修改后的代码可能会有所帮助。但猜测(以及从上面的代码)我猜 UI 没有时间更新视觉效果 - 因此没有进度更新。您需要在每次调用 progress 方法后允许 UI 更新。最好的方法是让进度移动操作发生在单独的线程上。
  • 我该怎么做
猜你喜欢
  • 2017-09-04
  • 2015-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 2012-06-11
  • 2018-02-20
  • 1970-01-01
相关资源
最近更新 更多