如何将在另一个类中创建和运行的后台工作程序的进度报告给UI

时间:2013-02-23 06:38:22

标签: c# .net winforms backgroundworker

我有一个调用类来启动后台工作程序的按钮。一切正常,除了我想报告进度到UI(有按钮)进行更新后,后台工作进度发生了变化。我尝试了很多东西但是没有成功。改变的进展不仅仅是火上浇油。

以下是启动后台工作人员的代码:

var backgroundWorker = new BackgroundWorker();
            ListBackgroundWorkerRunning.Add(path, backgroundWorker);
            backgroundWorker.WorkerReportsProgress = true;
            backgroundWorker.DoWork += (sender, e) =>
                {
                    _fileUploadRepository.UploadFiles(path);
                    var directoryConfiguration = new DirectoryConfiguration();
                    directoryConfiguration.UpdateProgressBarHandler(10);
                    //BackgroundWorker worker = sender as BackgroundWorker;

                    //directoryConfiguration.ProgressChanged += directoryConfiguration_ProgressChanged;
                };
            //backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
            backgroundWorker.RunWorkerCompleted += (sender, e) =>
                {
                    _crudOperation.UpdateDatabaseWithCrawlFinishedNotification(path);
                    RemoveCrawler(path);
                    InitializeWatcher(path);
                };
            backgroundWorker.RunWorkerAsync();

这是progress_changed事件:

//void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        //{
        //    var directoryConfiguration = new DirectoryConfiguration();
        //    directoryConfiguration.Invoke(new Action(() => directoryConfiguration.progressBar1.Value = e.ProgressPercentage));
        //}

1 个答案:

答案 0 :(得分:0)

您仍然需要致电

backgroundWorker.ReportProgress(currentProgress);

来自您的DoWork处理程序,以便触发进度事件。

http://msdn.microsoft.com/en-us/library/ka89zff4.aspx

您还必须再次在ProgressChanged处理程序中发表评论: - )