How can I report back on the progress of work carried out by a BackgroundWorker in an object in a class library?
I'm so far not sure how to use ReportProgress to feed back the information (can't refer to the calling class, due to circular dependency).
This is an example of the main project which initiates the work:
namespace MainProject {
class MainWindowVM : INotifyPropertyChanged {
private BackgroundWorker _counterWorker;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private int _progress;
public int Progress {
get { return _progress; }
set { _progress = value; NotifyPropertyChanged(); }
}
public MainWindowVM() {
var heavyWorker = new ClassLibrary.HeavyWorkerClass();
_counterWorker = new BackgroundWorker();
_counterWorker.DoWork += new DoWorkEventHandler(_counterWorker_DoWork);
_counterWorker.RunWorkerAsync(heavyWorker);
}
private void _counterWorker_DoWork(object sender, DoWorkEventArgs e) {
var heavyWorker = (ClassLibrary.HeavyWorkerClass)e.Argument;
heavyWorker.StartWork();
Progress = 100;
System.Windows.MessageBox.Show("Work completed!");
}
}
}
And here's an example of the uploading method in a class library:
namespace ClassLibrary {
public class HeavyWorkerClass {
public void StartWork() {
for (int i = 0; i <= 100; i++) {
Thread.Sleep(50);
}
}
}
}