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
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/EnhancedProgress/HeuristicLab.Clients.Hive/3.3/HiveClient.cs

    r14901 r15415  
    265265
    266266        // upload Job
    267         refreshableJob.Progress.Status = "Uploading Job...";
     267        refreshableJob.Progress.Message = "Uploading Job...";
    268268        refreshableJob.Job.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddJob(refreshableJob.Job));
    269269        refreshableJob.Job = HiveServiceLocator.Instance.CallHiveService((s) => s.GetJob(refreshableJob.Job.Id)); // update owner and permissions
     
    275275
    276276        // upload plugins
    277         refreshableJob.Progress.Status = "Uploading plugins...";
     277        refreshableJob.Progress.Message = "Uploading plugins...";
    278278        this.OnlinePlugins = HiveServiceLocator.Instance.CallHiveService((s) => s.GetPlugins());
    279279        this.AlreadyUploadedPlugins = new List<Plugin>();
     
    283283
    284284        // upload tasks
    285         refreshableJob.Progress.Status = "Uploading tasks...";
     285        refreshableJob.Progress.Message = "Uploading tasks...";
    286286
    287287        var tasks = new List<TS.Task>();
     
    380380        lock (jobCountLocker) {
    381381          progress.ProgressValue = (double)taskCount[0] / totalJobCount;
    382           progress.Status = string.Format("Uploaded task ({0} of {1})", taskCount[0], totalJobCount);
     382          progress.Message = string.Format("Uploaded task ({0} of {1})", taskCount[0], totalJobCount);
    383383        }
    384384
     
    416416        totalJobCount = allTasks.Count();
    417417
    418         refreshableJob.Progress.Status = "Downloading tasks...";
     418        refreshableJob.Progress.Message = "Downloading tasks...";
    419419        downloader = new TaskDownloader(allTasks.Select(x => x.Id));
    420420        downloader.StartAsync();
     
    422422        while (!downloader.IsFinished) {
    423423          refreshableJob.Progress.ProgressValue = downloader.FinishedCount / (double)totalJobCount;
    424           refreshableJob.Progress.Status = string.Format("Downloading/deserializing tasks... ({0}/{1} finished)", downloader.FinishedCount, totalJobCount);
     424          refreshableJob.Progress.Message = string.Format("Downloading/deserializing tasks... ({0}/{1} finished)", downloader.FinishedCount, totalJobCount);
    425425          Thread.Sleep(500);
    426426
     
    432432        var parents = allHiveTasks.Values.Where(x => !x.Task.ParentTaskId.HasValue);
    433433
    434         refreshableJob.Progress.Status = "Downloading/deserializing complete. Displaying tasks...";
     434        refreshableJob.Progress.Message = "Downloading/deserializing complete. Displaying tasks...";
    435435        // build child-task tree
    436436        foreach (HiveTask hiveTask in parents) {
  • branches/EnhancedProgress/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs

    r15400 r15415  
    2121
    2222using System;
     23using System.Runtime.InteropServices;
    2324using System.Windows.Forms;
    2425
     
    4041    public ProgressView(Control control, IProgress content)
    4142      : base() {
    42       if (control == null) throw new ArgumentNullException("control", "The control is null.");
    43       if (content == null) throw new ArgumentNullException("content", "The passed progress is null.");
     43      if (control == null) throw new ArgumentNullException("control");
     44      if (content == null) throw new ArgumentNullException("content");
    4445      InitializeComponent();
    4546
    4647      this.control = control;
    4748      this.content = content;
    48       if (content.ProgressState == ProgressState.Started)
    49         ShowProgress();
     49
     50      UpdateButtonsState();
     51      if (content.Visible) ShowProgress();
    5052      RegisterContentEvents();
    5153    }
     
    6668
    6769    private void RegisterContentEvents() {
    68       content.StatusChanged += new EventHandler(progress_StatusChanged);
    69       content.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
    7070      content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
     71      content.MessageChanged += new EventHandler(Content_MessageChanged);
     72      content.ProgressBarModeChanged += new EventHandler(Content_ProgressBarModeChanged);
     73      content.ProgressValueChanged += new EventHandler(Content_ProgressValueChanged);
     74      content.VisibleChanged += new EventHandler(Content_VisibleChanged);
     75      content.CanBeStoppedChanged += new EventHandler(Content_CanBeStoppedChanged);
    7176      content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
    7277    }
    7378    private void DeregisterContentEvents() {
    74       content.StatusChanged -= new EventHandler(progress_StatusChanged);
    75       content.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
    7679      content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
     80      content.MessageChanged -= new EventHandler(Content_MessageChanged);
     81      content.ProgressBarModeChanged -= new EventHandler(Content_ProgressBarModeChanged);
     82      content.ProgressValueChanged -= new EventHandler(Content_ProgressValueChanged);
     83      content.VisibleChanged -= new EventHandler(Content_VisibleChanged);
     84      content.CanBeStoppedChanged -= new EventHandler(Content_CanBeStoppedChanged);
    7785      content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
    7886    }
    7987
     88    private void Content_ProgressStateChanged(object sender, EventArgs e) {
     89      UpdateProgressState();
     90      UpdateButtonsState();
     91    }
     92
     93    private void Content_MessageChanged(object sender, EventArgs e) {
     94      UpdateProgressMessage();
     95    }
     96
     97    private void Content_ProgressBarModeChanged(object sender, EventArgs e) {
     98      UpdateProgressValue();
     99    }
     100    private void Content_ProgressValueChanged(object sender, EventArgs e) {
     101      UpdateProgressValue();
     102    }
     103
     104    private void Content_VisibleChanged(object sender, EventArgs e) {
     105      if (content.Visible)
     106        ShowProgress();
     107      else
     108        HideProgress();
     109    }
     110
     111    private void Content_CanBeStoppedChanged(object sender, EventArgs e) {
     112      UpdateButtonsState();
     113    }
     114    private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
     115      UpdateButtonsState();
     116    }
     117
    80118    private void ShowProgress() {
    81       if (Control.InvokeRequired) {
    82         Control.Invoke((Action)ShowProgress);
    83         return;
    84       }
    85       int height = Content.CanBeCanceled ? Height : collapsedControlHeight;
    86 
    87       Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
    88       Top = (Control.ClientRectangle.Height / 2) - (height / 2);
     119      if (control.InvokeRequired) {
     120        control.Invoke((Action)ShowProgress);
     121        return;
     122      }
     123
     124      int height = (content.CanBeStopped || content.CanBeCanceled) ? Height : collapsedControlHeight;
     125
     126      Left = (control.ClientRectangle.Width / 2) - (Width / 2);
     127      Top = (control.ClientRectangle.Height / 2) - (height / 2);
    89128      Anchor = AnchorStyles.None;
    90129
    91130      control.Enabled = false;
    92       Parent = Control.Parent;
     131      Parent = control.Parent;
    93132      BringToFront();
    94133
    95134      UpdateProgressValue();
    96       UpdateProgressStatus();
    97       UpdateCancelButton();
     135      UpdateProgressMessage();
     136      UpdateButtonsState();
    98137      Visible = true;
    99138    }
    100139
    101140    private void HideProgress() {
    102       if (InvokeRequired) Invoke((Action)HideProgress);
    103       else {
    104         control.Enabled = true;
    105         Parent = null;
    106         Visible = false;
    107       }
    108     }
    109 
    110     private void progress_StatusChanged(object sender, EventArgs e) {
    111       UpdateProgressStatus();
    112     }
    113 
    114     private void progress_ProgressValueChanged(object sender, EventArgs e) {
    115       UpdateProgressValue();
    116     }
    117 
    118     private void Content_ProgressStateChanged(object sender, EventArgs e) {
     141      if (control.InvokeRequired) {
     142        control.Invoke((Action)HideProgress);
     143        return;
     144      }
     145
     146      control.Enabled = true;
     147      Parent = null;
     148      Visible = false;
     149    }
     150
     151    private void UpdateProgressState() {
     152      if (control.InvokeRequired) {
     153        control.Invoke((Action)UpdateProgressState);
     154        return;
     155      }
     156
    119157      switch (content.ProgressState) {
    120         case ProgressState.Finished: HideProgress(); break;
    121         case ProgressState.Canceled: HideProgress(); break;
    122         case ProgressState.Started: ShowProgress(); break;
     158        case ProgressState.Started:
     159          progressBar.SetState(ProgressBarState.Normal);
     160          break;
     161        case ProgressState.Finished:
     162          HideProgress();
     163          progressBar.SetState(ProgressBarState.Normal);
     164          break;
     165        case ProgressState.Stopped:
     166          progressBar.SetState(ProgressBarState.Warning);
     167          break;
     168        case ProgressState.Canceled:
     169          progressBar.SetState(ProgressBarState.Error);
     170          break;
    123171        default: throw new NotSupportedException("The progress state " + content.ProgressState + " is not supported by the ProgressView.");
    124172      }
    125173    }
    126174
    127     private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
    128       UpdateCancelButton();
    129     }
    130 
    131     private void UpdateCancelButton() {
    132       cancelButton.Visible = content != null && content.CanBeCanceled;
    133       cancelButton.Enabled = content != null && content.CanBeCanceled;
    134 
    135       if (content != null && content.CanBeCanceled) {
    136         Height = defaultControlHeight;
    137       } else if (content != null && !content.CanBeCanceled) {
    138         Height = collapsedControlHeight;
    139       }
    140     }
     175    private void UpdateProgressMessage() {
     176      if (control.InvokeRequired) {
     177        control.Invoke((Action)UpdateProgressMessage);
     178        return;
     179      }
     180
     181      messageLabel.Text = content.Message;
     182    }
     183
    141184
    142185    private void UpdateProgressValue() {
     
    146189        try {
    147190          Invoke((Action)UpdateProgressValue);
    148         }
    149         catch (InvalidOperationException) {
     191        } catch (InvalidOperationException) {
    150192          // swallow ObjectDisposedException
    151193          // which might occur if the invoke call is executed after or while the control is disposing
    152194        }
     195        return;
     196      }
     197
     198      if (content.ProgressBarMode == ProgressBarMode.Continuous) {
     199        progressBar.Style = ProgressBarStyle.Continuous;
     200        progressBar.Value = (int)Math.Round(progressBar.Minimum + content.ProgressValue * (progressBar.Maximum - progressBar.Minimum));
    153201      } else {
    154         if (content != null) {
    155           double progressValue = content.ProgressValue;
    156           if (progressValue <= 0.0 || progressValue > 1.0) {
    157             progressBar.Style = ProgressBarStyle.Marquee;
    158           } else {
    159             progressBar.Style = ProgressBarStyle.Blocks;
    160             progressBar.Value =
    161               (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
    162           }
    163         }
    164       }
    165     }
    166 
    167     private void UpdateProgressStatus() {
    168       if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
    169       else if (content != null)
    170         statusLabel.Text = content.Status;
    171     }
    172 
     202        progressBar.Style = ProgressBarStyle.Marquee;
     203      }
     204    }
     205
     206    private void UpdateButtonsState() {
     207      if (control.InvokeRequired) {
     208        control.Invoke((Action)UpdateButtonsState);
     209        return;
     210      }
     211
     212      stopButton.Visible = content.CanBeStopped;
     213      stopButton.Enabled = content.ProgressState == ProgressState.Started && content.CanBeStopped;
     214
     215      cancelButton.Visible = content.CanBeCanceled;
     216      cancelButton.Enabled = content.ProgressState == ProgressState.Started && content.CanBeCanceled;
     217
     218      if (content.CanBeStopped || content.CanBeCanceled) {
     219        Height = defaultControlHeight;
     220      } else {
     221        Height = collapsedControlHeight;
     222      }
     223    }
     224
     225    private void stopButton_Click(object sender, EventArgs e) {
     226      content.Stop();
     227    }
    173228    private void cancelButton_Click(object sender, EventArgs e) {
    174229      content.Cancel();
    175230    }
    176231  }
     232
     233  // https://stackoverflow.com/a/9753302
     234  internal enum ProgressBarState {
     235    Normal = 1, Error = 2, Warning = 3
     236  }
     237  internal static class ProgressBarStateExtension {
     238    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
     239    private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr w, IntPtr l);
     240    public static void SetState(this ProgressBar pBar, ProgressBarState state) {
     241      SendMessage(pBar.Handle, 1040, (IntPtr)state, IntPtr.Zero);
     242    }
     243  }
    177244}
  • branches/EnhancedProgress/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.designer.cs

    r14185 r15415  
    2727    private System.ComponentModel.IContainer components = null;
    2828
    29    
     29
    3030    #region Component Designer generated code
    3131
     
    3636    private void InitializeComponent() {
    3737      this.progressBar = new System.Windows.Forms.ProgressBar();
    38       this.statusLabel = new System.Windows.Forms.Label();
     38      this.messageLabel = new System.Windows.Forms.Label();
    3939      this.cancelButton = new System.Windows.Forms.Button();
    4040      this.panel = new System.Windows.Forms.Panel();
     41      this.flowLayoutPanel = new System.Windows.Forms.FlowLayoutPanel();
     42      this.stopButton = new System.Windows.Forms.Button();
    4143      this.panel.SuspendLayout();
     44      this.flowLayoutPanel.SuspendLayout();
    4245      this.SuspendLayout();
    4346      //
     
    5255      this.progressBar.TabIndex = 0;
    5356      //
    54       // statusLabel
     57      // messageLabel
    5558      //
    56       this.statusLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
     59      this.messageLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
    5760            | System.Windows.Forms.AnchorStyles.Right)));
    58       this.statusLabel.Location = new System.Drawing.Point(3, 33);
    59       this.statusLabel.Name = "statusLabel";
    60       this.statusLabel.Size = new System.Drawing.Size(352, 17);
    61       this.statusLabel.TabIndex = 1;
     61      this.messageLabel.Location = new System.Drawing.Point(3, 33);
     62      this.messageLabel.Name = "messageLabel";
     63      this.messageLabel.Size = new System.Drawing.Size(352, 17);
     64      this.messageLabel.TabIndex = 1;
    6265      //
    6366      // cancelButton
    6467      //
    65       this.cancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
    66       this.cancelButton.Location = new System.Drawing.Point(280, 53);
     68      this.cancelButton.Location = new System.Drawing.Point(274, 3);
    6769      this.cancelButton.Name = "cancelButton";
    6870      this.cancelButton.Size = new System.Drawing.Size(75, 23);
     
    7880            | System.Windows.Forms.AnchorStyles.Right)));
    7981      this.panel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
     82      this.panel.Controls.Add(this.flowLayoutPanel);
    8083      this.panel.Controls.Add(this.progressBar);
    81       this.panel.Controls.Add(this.cancelButton);
    82       this.panel.Controls.Add(this.statusLabel);
     84      this.panel.Controls.Add(this.messageLabel);
    8385      this.panel.Location = new System.Drawing.Point(0, 0);
    8486      this.panel.Name = "panel";
    8587      this.panel.Size = new System.Drawing.Size(360, 88);
    8688      this.panel.TabIndex = 3;
     89      //
     90      // flowLayoutPanel
     91      //
     92      this.flowLayoutPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     93            | System.Windows.Forms.AnchorStyles.Left)
     94            | System.Windows.Forms.AnchorStyles.Right)));
     95      this.flowLayoutPanel.Controls.Add(this.cancelButton);
     96      this.flowLayoutPanel.Controls.Add(this.stopButton);
     97      this.flowLayoutPanel.Location = new System.Drawing.Point(3, 53);
     98      this.flowLayoutPanel.Name = "flowLayoutPanel";
     99      this.flowLayoutPanel.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
     100      this.flowLayoutPanel.Size = new System.Drawing.Size(352, 30);
     101      this.flowLayoutPanel.TabIndex = 3;
     102      //
     103      // stopButton
     104      //
     105      this.stopButton.Dock = System.Windows.Forms.DockStyle.Left;
     106      this.stopButton.Location = new System.Drawing.Point(193, 3);
     107      this.stopButton.Name = "stopButton";
     108      this.stopButton.Size = new System.Drawing.Size(75, 23);
     109      this.stopButton.TabIndex = 3;
     110      this.stopButton.Text = "Stop";
     111      this.stopButton.UseVisualStyleBackColor = true;
     112      this.stopButton.Click += new System.EventHandler(this.stopButton_Click);
    87113      //
    88114      // ProgressView
     
    93119      this.Size = new System.Drawing.Size(360, 88);
    94120      this.panel.ResumeLayout(false);
     121      this.flowLayoutPanel.ResumeLayout(false);
    95122      this.ResumeLayout(false);
    96123
     
    100127
    101128    private System.Windows.Forms.ProgressBar progressBar;
    102     private System.Windows.Forms.Label statusLabel;
     129    private System.Windows.Forms.Label messageLabel;
    103130    private System.Windows.Forms.Button cancelButton;
    104131    private System.Windows.Forms.Panel panel;
     132    private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel;
     133    private System.Windows.Forms.Button stopButton;
    105134  }
    106135}
  • branches/EnhancedProgress/HeuristicLab.MainForm.WindowsForms/3.3/MainForms/MainForm.cs

    r15400 r15415  
    353353    /// Adds a <see cref="ProgressView"/> to the <see cref="ContentView"/>s showing the specified content.
    354354    /// </summary>
     355    [Obsolete]
    355356    public IProgress AddOperationProgressToContent(IContent content, string progressMessage, bool addToObjectGraphObjects = true) {
    356357      if (InvokeRequired) {
     
    371372        contentViews = contentViews.Where(v => v.Content == content);
    372373
    373       var progress = new Progress(progressMessage, ProgressState.Started);
     374      var progress = new Progress();
     375      progress.Start(progressMessage);
    374376      foreach (var contentView in contentViews) {
    375377        progressViews.Add(new ProgressView(contentView, progress));
     
    383385    /// Adds a <see cref="ProgressView"/> to the specified view.
    384386    /// </summary>
     387    [Obsolete]
    385388    public IProgress AddOperationProgressToView(Control control, string progressMessage) {
    386       var progress = new Progress(progressMessage, ProgressState.Started);
     389      var progress = new Progress();
     390      progress.Start(progressMessage);
    387391      AddOperationProgressToView(control, progress);
    388392      return progress;
  • 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
  • branches/EnhancedProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs

    r15400 r15415  
    4040    private readonly ISymbolicDataAnalysisSolutionImpactValuesCalculator impactCalculator;
    4141
    42     private readonly IProgress progress = new Progress();
     42    private readonly IProgress progress = new Progress() { CanBeCanceled = true, CanBeStopped = true };
    4343
    4444    private enum TreeState { Valid, Invalid }
     
    183183      treeChart.Tree = tree.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(tree.Root) : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0));
    184184
    185       progress.Start("Calculate Impact and Replacement Values ...");
     185      progress.Start("Calculate Impact and Replacement Values ...", 0);
    186186      var impactAndReplacementValues = await Task.Run(() => CalculateImpactAndReplacementValues(tree));
    187187      await Task.Delay(500); // wait for progressbar to finish animation
     
    298298
    299299    private async void btnOptimizeConstants_Click(object sender, EventArgs e) {
    300       progress.Start("Optimizing Constants ...");
     300      progress.Start("Optimizing Constants ...", 0);
    301301      var tree = (ISymbolicExpressionTree)Content.Model.SymbolicExpressionTree.Clone();
    302302      var newTree = await Task.Run(() => OptimizeConstants(tree, progress));
Note: See TracChangeset for help on using the changeset viewer.