0

我有一个按钮,它调用一个类来启动一个后台工作人员。一切正常,除了我想向 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));
        //}
4

1 回答 1

0

你还是要打电话

backgroundWorker.ReportProgress(currentProgress);

不时从您的 DoWork 处理程序中触发进度事件。

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

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

于 2013-02-23T06:42:48.833 回答