Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm/3.3/Progress.cs @ 15371

Last change on this file since 15371 was 15371, checked in by pfleck, 7 years ago

#1666: Merged into trunk

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23
24namespace HeuristicLab.MainForm {
25  public class Progress : IProgress {
26    private string status;
27    public string Status {
28      get { return status; }
29      set {
30        if (status != value) {
31          status = value;
32          OnStatusChanged();
33        }
34      }
35    }
36
37    private double progressValue;
38    public double ProgressValue {
39      get { return progressValue; }
40      set {
41        if (progressValue != value) {
42          progressValue = value;
43          OnProgressChanged();
44        }
45      }
46    }
47
48    private ProgressState progressState;
49    public ProgressState ProgressState {
50      get { return progressState; }
51      private set {
52        if (progressState != value) {
53          progressState = value;
54          OnProgressStateChanged();
55        }
56      }
57    }
58
59    private bool canBeCanceled;
60    public bool CanBeCanceled {
61      get { return canBeCanceled; }
62      set {
63        if (canBeCanceled != value) {
64          canBeCanceled = value;
65          OnCanBeCanceledChanged();
66        }
67      }
68    }
69
70    public Progress() {
71      progressState = ProgressState.Finished;
72      canBeCanceled = false;
73    }
74
75    public void Cancel() {
76      if (canBeCanceled)
77        OnCancelRequested();
78    }
79
80    public void Finish() {
81      if (ProgressValue != 1.0) ProgressValue = 1.0;
82      ProgressState = ProgressState.Finished;
83    }
84
85    public void Start() {
86      Start(string.Empty);
87    }
88    public void Start(string status) {
89      Start(status, -1.0);
90    }
91    public void Start(string status, double progressValue) {
92      ProgressState = ProgressState.Started;
93      Status = status ?? string.Empty;
94      ProgressValue = progressValue;
95    }
96
97    #region Event Handler
98    public event EventHandler StatusChanged;
99    private void OnStatusChanged() {
100      var handler = StatusChanged;
101      if (handler != null) handler(this, EventArgs.Empty);
102    }
103
104    public event EventHandler ProgressValueChanged;
105    private void OnProgressChanged() {
106      var handler = ProgressValueChanged;
107      if (handler != null) handler(this, EventArgs.Empty);
108    }
109
110    public event EventHandler ProgressStateChanged;
111    private void OnProgressStateChanged() {
112      var handler = ProgressStateChanged;
113      if (handler != null) handler(this, EventArgs.Empty);
114    }
115
116    public event EventHandler CanBeCanceledChanged;
117    private void OnCanBeCanceledChanged() {
118      var handler = CanBeCanceledChanged;
119      if (handler != null) handler(this, EventArgs.Empty);
120    }
121
122    public event EventHandler CancelRequested;
123    private void OnCancelRequested() {
124      var handler = CancelRequested;
125      if (handler != null) throw new NotSupportedException("Cancel request was ignored.");
126      else handler(this, EventArgs.Empty);
127    }
128    #endregion
129  }
130}
Note: See TracBrowser for help on using the repository browser.