Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/10/17 14:30:37 (7 years ago)
Author:
pfleck
Message:

#2845

  • Fixed/Added Progress Cancellation/Stopping
  • Added Visible property to progress and ProgressBarMode.
Location:
branches/EnhancedProgress/HeuristicLab.MainForm/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/EnhancedProgress/HeuristicLab.MainForm/3.3/Interfaces/IProgress.cs

    r15400 r15415  
    2424
    2525namespace HeuristicLab.MainForm {
    26   public enum ProgressState { Started = 1, Canceled = 2, Finished = 3 };
     26  public enum ProgressState { Started, Finished, Stopped, Canceled }
     27  public enum ProgressBarMode { Continuous, Marquee }
    2728
    2829  public interface IProgress : IContent {
     30    ProgressState ProgressState { get; }
     31    string Message { get; set; }
     32    ProgressBarMode ProgressBarMode { get; set; }
    2933    /// <summary>
    30     /// Gets or sets the currently associated status text with the progress.
    31     /// </summary>
    32     string Status { get; set; }
    33     /// <summary>
    34     /// Gets or sets the currently associated progress value in the range (0;1].
    35     ///  Values outside this range are permitted and need to be handled in some feasible manner.
     34    /// Gets or sets the currently associated progress value in the range [0;1] (values outside the range are truncated).
     35    /// Changing the ProgressValue when ProgressBarMode is Marquee raises an Exception.
    3636    /// </summary>
    3737    double ProgressValue { get; set; }
    38     /// <summary>
    39     /// Gets or sets the current state of the progress. Every progress starts in state
    40     /// Started and then becomes either Canceled or Finished.
    41     /// If it is reused it may be Started again.
    42     /// </summary>
    43     ProgressState ProgressState { get; }
    44     /// <summary>
    45     /// Returns whether the operation can be canceled or not.
    46     /// This can change during the course of the progress.
    47     /// </summary>
     38    bool Visible { get; set; }
     39    bool CanBeStopped { get; }
    4840    bool CanBeCanceled { get; }
    4941
    5042    /// <summary>
    51     /// Requests the operation behind the process to cancel.
    52     /// Check the !ProgressState property when the cancellation succeeded.
    53     /// The corresponding event will also notify of a success.
     43    /// Start (or Restart) a progress with ProgressBarMode Marquee
    5444    /// </summary>
    55     /// <exception cref="NotSupportedException">Thrown when cancellation is not supported.</exception>
     45    void Start(string message);
     46    /// <summary>
     47    /// Start (or Restart) a progress with ProgressBarMode Continues
     48    /// </summary>
     49    void Start(string message, double progressValue);
     50    void Finish();
     51    void Stop();
    5652    void Cancel();
    57     /// <summary>
    58     /// Sets the ProgressValue to 1 and the ProgressState to Finished.
    59     /// </summary>
    60     void Finish();
    6153
    62     /// <summary>
    63     /// Starts or restarts a Progress.
    64     /// </summary>
    65     void Start();
    66 
    67     void Start(string status);
    68 
    69     /// <summary>
    70     /// The status text changed.
    71     /// </summary>
    72     event EventHandler StatusChanged;
    73     /// <summary>
    74     /// The value of the progress changed. This is the (0;1] progress value from starting to finish. Values outside this range are permitted and need to be handled in some feasible manner.
    75     /// </summary>
     54    event EventHandler ProgressStateChanged;
     55    event EventHandler MessageChanged;
     56    event EventHandler ProgressBarModeChanged;
    7657    event EventHandler ProgressValueChanged;
    77     /// <summary>
    78     /// The state of the progress changed. The handler is supposed to query the ProgressState property.
    79     /// </summary>
    80     event EventHandler ProgressStateChanged;
    81     /// <summary>
    82     /// The progress' ability to cancel changed.
    83     /// </summary>
     58    event EventHandler VisibleChanged;
     59    event EventHandler CanBeStoppedChanged;
    8460    event EventHandler CanBeCanceledChanged;
    85     /// <summary>
    86     /// A cancelation is requested.
    87     /// </summary>
     61    event EventHandler StopRequested;
    8862    event EventHandler CancelRequested;
    8963  }
  • branches/EnhancedProgress/HeuristicLab.MainForm/3.3/Progress.cs

    r15400 r15415  
    2424namespace HeuristicLab.MainForm {
    2525  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 
    4826    private ProgressState progressState;
    4927    public ProgressState ProgressState {
     
    5735    }
    5836
     37    private string message;
     38    public string Message {
     39      get { return message; }
     40      set {
     41        if (message != value) {
     42          message = value;
     43          OnMessageChanged();
     44        }
     45      }
     46    }
     47
     48    private ProgressBarMode progressBarMode;
     49    public ProgressBarMode ProgressBarMode {
     50      get { return progressBarMode; }
     51      set {
     52        if (progressBarMode != value) {
     53          progressBarMode = value;
     54          OnProgressBarModeChanged();
     55        }
     56      }
     57    }
     58
     59    private double progressValue;
     60    public double ProgressValue {
     61      get { return progressValue; }
     62      set {
     63        if (progressBarMode == ProgressBarMode.Marquee)
     64          throw new InvalidOperationException("Cannot set ProgressValue while ProgressBar is in Marquee-Mode");
     65        if (progressValue != value) {
     66          progressValue = Math.Max(Math.Min(value, 1.0), 0.0);
     67          OnProgressChanged();
     68        }
     69      }
     70    }
     71
     72    private bool visible;
     73    public bool Visible {
     74      get { return visible; }
     75      set {
     76        if (visible != value) {
     77          visible = value;
     78          OnVisibleChanged();
     79        }
     80      }
     81    }
     82
     83    private bool canBeStopped;
     84    public bool CanBeStopped {
     85      get { return canBeStopped; }
     86      set {
     87        if (canBeStopped != value) {
     88          canBeStopped = value;
     89          OnCanBeStoppedChanged();
     90        }
     91      }
     92    }
     93
    5994    private bool canBeCanceled;
    6095    public bool CanBeCanceled {
     
    70105    public Progress() {
    71106      progressState = ProgressState.Finished;
     107      canBeStopped = false;
    72108      canBeCanceled = false;
    73     }
    74     public Progress(string status)
    75       : this() {
    76       this.status = status;
    77     }
    78     public Progress(string status, ProgressState state)
    79       : this() {
    80       this.status = status;
    81       this.progressState = state;
    82     }
    83 
     109      progressBarMode = ProgressBarMode.Marquee;
     110      progressValue = 0.0;
     111    }
     112
     113    public void Start(string message) {
     114      ProgressState = ProgressState.Started;
     115      ProgressBarMode = ProgressBarMode.Marquee;
     116      Message = message;
     117      Visible = true;
     118    }
     119    public void Start(string message, double progressValue) {
     120      ProgressState = ProgressState.Started;
     121      ProgressBarMode = ProgressBarMode.Continuous;
     122      ProgressValue = progressValue;
     123      Message = message;
     124      Visible = true;
     125    }
     126
     127    public void Finish() {
     128      if (ProgressBarMode == ProgressBarMode.Continuous && ProgressValue != 1.0)
     129        ProgressValue = 1.0;
     130      ProgressState = ProgressState.Finished;
     131      Visible = false;
     132    }
     133
     134    public void Stop() {
     135      if (canBeStopped) {
     136        ProgressState = ProgressState.Stopped;
     137        OnStopRequested();
     138      } else throw new NotSupportedException("This progress cannot be stopped.");
     139    }
    84140    public void Cancel() {
    85       if (canBeCanceled)
     141      if (canBeCanceled) {
     142        ProgressState = ProgressState.Canceled;
    86143        OnCancelRequested();
    87     }
    88 
    89     public void Finish() {
    90       if (ProgressValue != 1.0) ProgressValue = 1.0;
    91       ProgressState = ProgressState.Finished;
    92     }
    93 
    94     public void Start() {
    95       ProgressValue = 0.0;
    96       ProgressState = ProgressState.Started;
    97     }
    98 
    99     public void Start(string status) {
    100       Start();
    101       Status = status;
     144      } else throw new NotSupportedException("This progress cannot be canceled.");
    102145    }
    103146
    104147    #region Event Handler
    105     public event EventHandler StatusChanged;
    106     private void OnStatusChanged() {
    107       var handler = StatusChanged;
     148    public event EventHandler ProgressStateChanged;
     149    private void OnProgressStateChanged() {
     150      var handler = ProgressStateChanged;
     151      if (handler != null) handler(this, EventArgs.Empty);
     152    }
     153
     154    public event EventHandler MessageChanged;
     155    private void OnMessageChanged() {
     156      var handler = MessageChanged;
     157      if (handler != null) handler(this, EventArgs.Empty);
     158    }
     159
     160    public event EventHandler ProgressBarModeChanged;
     161    private void OnProgressBarModeChanged() {
     162      var handler = ProgressBarModeChanged;
    108163      if (handler != null) handler(this, EventArgs.Empty);
    109164    }
     
    115170    }
    116171
    117     public event EventHandler ProgressStateChanged;
    118     private void OnProgressStateChanged() {
    119       var handler = ProgressStateChanged;
     172    public event EventHandler VisibleChanged;
     173    private void OnVisibleChanged() {
     174      var handler = VisibleChanged;
     175      if (handler != null) handler(this, EventArgs.Empty);
     176    }
     177
     178    public event EventHandler CanBeStoppedChanged;
     179    private void OnCanBeStoppedChanged() {
     180      var handler = CanBeStoppedChanged;
    120181      if (handler != null) handler(this, EventArgs.Empty);
    121182    }
     
    127188    }
    128189
     190    public event EventHandler StopRequested;
     191    private void OnStopRequested() {
     192      var handler = StopRequested;
     193      if (handler != null) handler(this, EventArgs.Empty);
     194    }
     195
    129196    public event EventHandler CancelRequested;
    130197    private void OnCancelRequested() {
    131198      var handler = CancelRequested;
    132       if (handler != null) throw new NotSupportedException("Cancel request was ignored.");
    133       else handler(this, EventArgs.Empty);
     199      if (handler != null) handler(this, EventArgs.Empty);
    134200    }
    135201    #endregion
Note: See TracChangeset for help on using the changeset viewer.