Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.MainForm/3.3/Progress.cs @ 11194

Last change on this file since 11194 was 8206, checked in by gkronber, 12 years ago

#1847: merged r8084:8205 from trunk into GP move operators branch

File size: 3.9 KB
RevLine 
[7582]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
[8206]23using HeuristicLab.Common;
[7582]24
25namespace HeuristicLab.MainForm {
26  public class Progress : IProgress {
27    private string status;
28    public string Status {
[8206]29      get { return status; }
[7582]30      set {
[8206]31        if (status != value) {
32          status = value;
[7582]33          OnStatusChanged();
34        }
35      }
36    }
37
38    private double progressValue;
39    public double ProgressValue {
[8206]40      get { return progressValue; }
[7582]41      set {
[8206]42        if (progressValue != value) {
43          progressValue = value;
[7582]44          OnProgressChanged();
45        }
46      }
47    }
48
[8206]49    private ProgressState progressState;
50    public ProgressState ProgressState {
51      get { return progressState; }
52      set {
53        if (progressState != value) {
54          progressState = value;
55          OnProgressStateChanged();
56        }
57      }
58    }
[7582]59
[8206]60    private bool canBeCanceled;
61    public bool CanBeCanceled {
62      get { return canBeCanceled; }
63      set {
64        if (canBeCanceled != value) {
65          canBeCanceled = value;
66          OnCanBeCanceledChanged();
67        }
68      }
[7582]69    }
70
[8206]71    public Progress() {
72      progressState = ProgressState.Started;
73    }
74    public Progress(string status)
75      : this() {
76      this.status = status;
77    }
78    public Progress(string status, double progressValue)
79      : this(status) {
80      this.progressValue = progressValue;
81    }
82
83    public void Cancel(int timeoutMs) {
84      if (canBeCanceled)
85        OnCancelRequested(timeoutMs);
86    }
87
88    /// <summary>
89    /// Sets the ProgressValue to 1 and the ProgressState to Finished.
90    /// </summary>
[7582]91    public void Finish() {
[8206]92      if (ProgressValue != 1.0) ProgressValue = 1.0;
93      ProgressState = ProgressState.Finished;
[7582]94    }
95
96    #region Event Handler
97    public event EventHandler StatusChanged;
98    private void OnStatusChanged() {
99      var handler = StatusChanged;
100      try {
101        if (handler != null) handler(this, EventArgs.Empty);
[8206]102      } catch { }
[7582]103    }
104
105    public event EventHandler ProgressValueChanged;
106    private void OnProgressChanged() {
107      var handler = ProgressValueChanged;
108      try {
109        if (handler != null) handler(this, EventArgs.Empty);
[8206]110      } catch { }
[7582]111    }
[8206]112
113    public event EventHandler ProgressStateChanged;
114    private void OnProgressStateChanged() {
115      var handler = ProgressStateChanged;
116      try {
117        if (handler != null) handler(this, EventArgs.Empty);
118      } catch { }
119    }
120
121    public event EventHandler CanBeCanceledChanged;
122    private void OnCanBeCanceledChanged() {
123      var handler = CanBeCanceledChanged;
124      try {
125        if (handler != null) handler(this, EventArgs.Empty);
126      } catch { }
127    }
128
129    public event EventHandler<EventArgs<int>> CancelRequested;
130    private void OnCancelRequested(int timeoutMs) {
131      var handler = CancelRequested;
132      try {
133        if (handler == null) throw new NotSupportedException("Cancel request was ignored.");
134        else handler(this, new EventArgs<int>(timeoutMs));
135      } catch { }
136    }
[7582]137    #endregion
138  }
139}
Note: See TracBrowser for help on using the repository browser.