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 {
|
---|
15 | public string Name { get; private set; }
|
---|
16 | public int Progress { get; private set; }
|
---|
17 |
|
---|
18 | /// <summary>
|
---|
19 | /// Indicate whether the worker is actually doing something. In contrast
|
---|
20 | /// to IsBusy this shows that the worker is about to execute or leave DoWork
|
---|
21 | /// while IsBusy shows wheter the worker has been "started" with this.RunWorkerAsync.
|
---|
22 | /// </summary>
|
---|
23 | public bool IsRunning { get; private set; }
|
---|
24 |
|
---|
25 | /// <summary>
|
---|
26 | /// Notification about worker has actually started working i.e.
|
---|
27 | /// has been assigned a ThreadPool thread.
|
---|
28 | /// </summary>
|
---|
29 | public event EventHandler WorkerStarted;
|
---|
30 |
|
---|
31 | /// <summary>
|
---|
32 | /// Unsynchronized version of RunWorkerCompleted so you don't
|
---|
33 | /// have to wait for the GUI thread to be notified.
|
---|
34 | /// </summary>
|
---|
35 | public event EventHandler WorkerStopped;
|
---|
36 |
|
---|
37 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
38 |
|
---|
39 | public ObservableBackgroundWorker(string name, WorkerMonitor monitor) {
|
---|
40 | Name = name;
|
---|
41 | monitor.RegisterWorker(this);
|
---|
42 | IsRunning = false;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public ObservableBackgroundWorker(string name) : this(name, WorkerMonitor.Default) { }
|
---|
46 |
|
---|
47 | protected override void OnProgressChanged(ProgressChangedEventArgs e) {
|
---|
48 | Progress = e.ProgressPercentage;
|
---|
49 | base.OnProgressChanged(e);
|
---|
50 | OnPropertyChanged("Progress");
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected override void OnDoWork(DoWorkEventArgs e) {
|
---|
54 | IsRunning = true;
|
---|
55 | try {
|
---|
56 | OnPropertyChanged("IsRunning");
|
---|
57 | OnWorkerStarted();
|
---|
58 | base.OnDoWork(e);
|
---|
59 | } finally {
|
---|
60 | IsRunning = false;
|
---|
61 | OnWorkerStopped();
|
---|
62 | OnPropertyChanged("IsRunning");
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected void OnWorkerStarted() {
|
---|
67 | if (WorkerStarted != null)
|
---|
68 | WorkerStarted(this, new EventArgs());
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected void OnWorkerStopped() {
|
---|
72 | if (WorkerStopped != null)
|
---|
73 | WorkerStopped(this, new EventArgs());
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected void OnPropertyChanged(string name) {
|
---|
77 | if (PropertyChanged != null)
|
---|
78 | PropertyChanged(this, new PropertyChangedEventArgs(name));
|
---|
79 | }
|
---|
80 |
|
---|
81 | public new void CancelAsync() {
|
---|
82 | base.CancelAsync();
|
---|
83 | OnPropertyChanged("CancellationPending");
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|