Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.BackgroundProcessing/ObservableBackgroundWorker.cs @ 6451

Last change on this file since 6451 was 4311, checked in by swagner, 14 years ago

Integrated OKB clients for HL 3.3 (#1166)

File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ComponentModel;
6using System.Threading;
7using System.Collections.ObjectModel;
8
9namespace 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
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
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
52    public ObservableBackgroundWorker(string name, WorkerMonitor monitor) {
53      Name = name;
54      monitor.RegisterWorker(this);
55      IsRunning = false;
56    }
57
58    public ObservableBackgroundWorker(string name) : this(name, WorkerMonitor.Default) { }
59
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    }
78
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}
Note: See TracBrowser for help on using the repository browser.