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