[2399] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.ComponentModel;
|
---|
| 6 | using System.Threading;
|
---|
| 7 | using System.Collections.ObjectModel;
|
---|
| 8 |
|
---|
| 9 | namespace HeuristicLab.BackgroundProcessing {
|
---|
| 10 | /// <summary>
|
---|
| 11 | /// Extends the BackgroundWorker to make it easier to follow progress and automatically
|
---|
| 12 | /// registers with the WorkerMonitor.
|
---|
| 13 | /// </summary>
|
---|
| 14 | public class ObservableBackgroundWorker : BackgroundWorker, INotifyPropertyChanged {
|
---|
[2473] | 15 |
|
---|
| 16 | private string name;
|
---|
| 17 | public string Name {
|
---|
| 18 | get {
|
---|
| 19 | return name;
|
---|
| 20 | }
|
---|
| 21 | set {
|
---|
| 22 | if (value == name)
|
---|
| 23 | return;
|
---|
| 24 | name = value;
|
---|
| 25 | OnPropertyChanged("Name");
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[2399] | 29 | public int Progress { get; private set; }
|
---|
| 30 |
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// Indicate whether the worker is actually doing something. In contrast
|
---|
| 33 | /// to IsBusy this shows that the worker is about to execute or leave DoWork
|
---|
| 34 | /// while IsBusy shows wheter the worker has been "started" with this.RunWorkerAsync.
|
---|
| 35 | /// </summary>
|
---|
| 36 | public bool IsRunning { get; private set; }
|
---|
| 37 |
|
---|
| 38 | /// <summary>
|
---|
| 39 | /// Notification about worker has actually started working i.e.
|
---|
| 40 | /// has been assigned a ThreadPool thread.
|
---|
| 41 | /// </summary>
|
---|
| 42 | public event EventHandler WorkerStarted;
|
---|
| 43 |
|
---|
| 44 | /// <summary>
|
---|
| 45 | /// Unsynchronized version of RunWorkerCompleted so you don't
|
---|
| 46 | /// have to wait for the GUI thread to be notified.
|
---|
| 47 | /// </summary>
|
---|
| 48 | public event EventHandler WorkerStopped;
|
---|
| 49 |
|
---|
| 50 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
| 51 |
|
---|
[2410] | 52 | public ObservableBackgroundWorker(string name, WorkerMonitor monitor) {
|
---|
[2399] | 53 | Name = name;
|
---|
[2410] | 54 | monitor.RegisterWorker(this);
|
---|
[2399] | 55 | IsRunning = false;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[2410] | 58 | public ObservableBackgroundWorker(string name) : this(name, WorkerMonitor.Default) { }
|
---|
| 59 |
|
---|
[2399] | 60 | protected override void OnProgressChanged(ProgressChangedEventArgs e) {
|
---|
| 61 | Progress = e.ProgressPercentage;
|
---|
| 62 | base.OnProgressChanged(e);
|
---|
| 63 | OnPropertyChanged("Progress");
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | protected override void OnDoWork(DoWorkEventArgs e) {
|
---|
| 67 | IsRunning = true;
|
---|
| 68 | try {
|
---|
| 69 | OnPropertyChanged("IsRunning");
|
---|
| 70 | OnWorkerStarted();
|
---|
| 71 | base.OnDoWork(e);
|
---|
| 72 | } finally {
|
---|
| 73 | IsRunning = false;
|
---|
| 74 | OnWorkerStopped();
|
---|
| 75 | OnPropertyChanged("IsRunning");
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
[2408] | 78 |
|
---|
[2399] | 79 | protected void OnWorkerStarted() {
|
---|
| 80 | if (WorkerStarted != null)
|
---|
| 81 | WorkerStarted(this, new EventArgs());
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | protected void OnWorkerStopped() {
|
---|
| 85 | if (WorkerStopped != null)
|
---|
| 86 | WorkerStopped(this, new EventArgs());
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | protected void OnPropertyChanged(string name) {
|
---|
| 90 | if (PropertyChanged != null)
|
---|
| 91 | PropertyChanged(this, new PropertyChangedEventArgs(name));
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public new void CancelAsync() {
|
---|
| 95 | base.CancelAsync();
|
---|
| 96 | OnPropertyChanged("CancellationPending");
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|