Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8165


Ignore:
Timestamp:
06/29/12 23:12:32 (12 years ago)
Author:
abeham
Message:

#1762: Some changes to progress handling, see the ticket for more details

Location:
trunk/sources
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Clients.Access.Views/3.3/ClientViews/ClientView.cs

    r8160 r8165  
    3535
    3636    private ProgressView progressView;
    37     IProgress progress;
     37    private Progress progress;
    3838
    3939    public ClientView() {
    4040      InitializeComponent();
    41       progressView = new ProgressView(this);
     41      progress = new Progress() {
     42        CanBeCanceled = false,
     43        ProgressState = ProgressState.Finished
     44      };
     45    }
     46
     47    protected override void DeregisterContentEvents() {
     48      if (progressView != null) {
     49        progressView.Content = null;
     50        progressView.Dispose();
     51        progressView = null;
     52      }
     53      base.DeregisterContentEvents();
     54    }
     55
     56    protected override void RegisterContentEvents() {
     57      base.RegisterContentEvents();
     58      progressView = new ProgressView(this, progress);
    4259    }
    4360
     
    84101        Invoke(new Action(StartProgressView));
    85102      } else {
    86         progress = new Progress();
    87103        progress.Status = "Downloading client information. Please be patient.";
    88         progressView.Progress = progress;
     104        progress.ProgressState = ProgressState.Started;
    89105      }
    90106    }
  • trunk/sources/HeuristicLab.Clients.Hive.JobManager/3.3/Views/HiveResourceSelector.cs

    r8160 r8165  
    4242    private ISet<TreeNode> filteredTreeNodes;
    4343    private ISet<TreeNode> nodeStore;
    44     private IProgress currentProgress;
     44    private Progress progress;
     45    private ProgressView progressView;
    4546
    4647    private ISet<Resource> selectedResources;
     
    4849      get { return selectedResources; }
    4950      set { selectedResources = value; }
    50     }
    51 
    52     private ProgressView progressView;
    53     public ProgressView ProgressView {
    54       get { return progressView; }
    55       set { progressView = value; }
    5651    }
    5752
     
    6964      imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.MonitorLarge);
    7065      imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.NetworkCenterLarge);
     66      progress = new Progress() {
     67        CanBeCanceled = false,
     68        ProgressState = ProgressState.Finished
     69      };
     70    }
     71
     72    protected override void DeregisterContentEvents() {
     73      if (progressView != null) {
     74        progressView.Content = null;
     75        progressView.Dispose();
     76        progressView = null;
     77      }
     78      base.DeregisterContentEvents();
     79    }
     80
     81    protected override void RegisterContentEvents() {
     82      base.RegisterContentEvents();
     83      progressView = new ProgressView(this, progress);
    7184    }
    7285
     
    7588        Invoke(new Action(StartProgressView));
    7689      } else {
    77         currentProgress = new Progress();
    78         currentProgress.Status = "Downloading resources. Please be patient.";
    79         if (progressView == null) {
    80           progressView = new ProgressView(this);
    81         }
    82         progressView.Progress = currentProgress;
     90        progress.Status = "Downloading resources. Please be patient.";
     91        progress.ProgressState = ProgressState.Started;
    8392      }
    8493    }
     
    8897        Invoke(new Action(FinishProgressView));
    8998      } else {
    90         currentProgress.Finish();
     99        progress.Finish();
    91100      }
    92101    }
  • trunk/sources/HeuristicLab.Clients.Hive.JobManager/3.3/Views/HiveResourceSelectorDialog.Designer.cs

    r7967 r8165  
    7373      this.hiveResourceSelector.Location = new System.Drawing.Point(12, 12);
    7474      this.hiveResourceSelector.Name = "hiveResourceSelector";
    75       this.hiveResourceSelector.ProgressView = null;
    7675      this.hiveResourceSelector.ReadOnly = false;
    7776      this.hiveResourceSelector.Size = new System.Drawing.Size(549, 591);
  • trunk/sources/HeuristicLab.Clients.Hive.JobManager/3.3/Views/RefreshableHiveJobView.cs

    r8160 r8165  
    4242  [Content(typeof(RefreshableJob), true)]
    4343  public partial class RefreshableHiveJobView : HeuristicLab.Core.Views.ItemView {
     44    private Progress progress;
    4445    private ProgressView progressView;
    4546    private HiveResourceSelectorDialog hiveResourceSelectorDialog;
     
    5556    public RefreshableHiveJobView() {
    5657      InitializeComponent();
    57       progressView = new ProgressView(this);
     58      progress = new Progress() {
     59        CanBeCanceled = true,
     60        ProgressState = ProgressState.Finished
     61      };
    5862    }
    5963
     
    7377      Content.Loaded += new EventHandler(Content_Loaded);
    7478      Content.TaskReceived += new EventHandler(Content_TaskReceived);
     79      progressView = new ProgressView(this, progress);
    7580    }
    7681
     
    8893      Content.Loaded -= new EventHandler(Content_Loaded);
    8994      Content.TaskReceived -= new EventHandler(Content_TaskReceived);
     95      if (progressView != null) {
     96        progressView.Content = null;
     97        progressView.Dispose();
     98        progressView = null;
     99      }
    90100      base.DeregisterContentEvents();
    91101    }
     
    356366        var task = System.Threading.Tasks.Task.Factory.StartNew(ResumeJobAsync, Content);
    357367        task.ContinueWith((t) => {
    358           FinishProgressView();
     368          progress.Finish();
    359369          MessageBox.Show("An error occured resuming the job. See the log for more information.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
    360370          Content.Log.LogException(t.Exception);
     
    368378      var task = System.Threading.Tasks.Task.Factory.StartNew(PauseJobAsync, Content);
    369379      task.ContinueWith((t) => {
    370         FinishProgressView();
     380        progress.Finish();
    371381        MessageBox.Show("An error occured pausing the job. See the log for more information.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
    372382        Content.Log.LogException(t.Exception);
     
    377387      var task = System.Threading.Tasks.Task.Factory.StartNew(StopJobAsync, Content);
    378388      task.ContinueWith((t) => {
    379         FinishProgressView();
     389        progress.Finish();
    380390        MessageBox.Show("An error occured stopping the job. See the log for more information.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
    381391        Content.Log.LogException(t.Exception);
     
    385395
    386396    private void PauseJobAsync(object job) {
    387       IProgress prog = new Progress();
    388       prog.Status = "Pausing job...";
    389       SetProgressView(prog);
     397      progress.Status = "Pausing job...";
     398      progress.ProgressState = ProgressState.Started;
    390399      HiveClient.PauseJob((RefreshableJob)job);
    391       FinishProgressView();
    392400    }
    393401
    394402    private void StopJobAsync(object job) {
    395       IProgress prog = new Progress();
    396       prog.Status = "Stopping job...";
    397       SetProgressView(prog);
     403      progress.Status = "Stopping job...";
     404      progress.ProgressState = ProgressState.Started;
    398405      HiveClient.StopJob((RefreshableJob)job);
    399       FinishProgressView();
    400406    }
    401407
    402408    private void ResumeJobAsync(object job) {
    403       IProgress prog = new Progress();
    404       prog.Status = "Resuming job...";
    405       SetProgressView(prog);
     409      progress.Status = "Resuming job...";
     410      progress.ProgressState = ProgressState.Started;
    406411      HiveClient.ResumeJob((RefreshableJob)job);
    407       FinishProgressView();
    408412    }
    409413
     
    466470      } else {
    467471        if (Content != null && Content.Progress != null && Content.IsProgressing) {
    468           SetProgressView();
     472          progressView.Content = Content.Progress;
    469473        } else {
    470           progressView.Progress = null;
    471         }
    472       }
    473     }
    474 
    475     private void SetProgressView() {
    476       if (InvokeRequired) {
    477         Invoke(new Action(SetProgressView));
    478       } else {
    479         progressView.Progress = Content.Progress;
    480       }
    481     }
    482 
    483     private void SetProgressView(IProgress progress) {
    484       if (InvokeRequired) {
    485         Invoke(new Action<IProgress>(SetProgressView), progress);
    486       } else {
    487         progressView.Progress = progress;
    488       }
    489     }
    490 
    491     private void FinishProgressView() {
    492       if (InvokeRequired) {
    493         Invoke(new Action(FinishProgressView));
    494       } else {
    495         if (Content != null && Content.Progress != null)
    496           Content.Progress.Finish();
     474          progressView.Content = progress;
     475        }
    497476      }
    498477    }
  • trunk/sources/HeuristicLab.Clients.Hive.Views/3.3/HiveTasks/OptimizerHiveTaskView.cs

    r8160 r8165  
    3131  [Content(typeof(OptimizerHiveTask), true)]
    3232  public partial class OptimizerHiveTaskView : HiveTaskView {
    33     private ProgressView progressView;
    34     private IProgress progress;
     33    private Progress progress;
    3534
    3635    public new OptimizerHiveTask Content {
     
    4544    public OptimizerHiveTaskView() {
    4645      InitializeComponent();
    47       progressView = new ProgressView(this);
     46      progress = new Progress() {
     47        CanBeCanceled = false
     48      };
    4849    }
    4950
     
    7677      var task = System.Threading.Tasks.Task.Factory.StartNew(ResumeTaskAsync);
    7778      task.ContinueWith((t) => {
    78         FinishProgressView();
     79        progress.Finish();
    7980        ErrorHandling.ShowErrorDialog(this, "An error occured while resuming the task.", t.Exception);
    8081      }, TaskContinuationOptions.OnlyOnFaulted);
     
    8485      var task = System.Threading.Tasks.Task.Factory.StartNew(PauseTaskAsync);
    8586      task.ContinueWith((t) => {
    86         FinishProgressView();
     87        progress.Finish();
    8788        ErrorHandling.ShowErrorDialog(this, "An error occured while pausing the task.", t.Exception);
    8889      }, TaskContinuationOptions.OnlyOnFaulted);
     
    9293      var task = System.Threading.Tasks.Task.Factory.StartNew(StopTaskAsync);
    9394      task.ContinueWith((t) => {
    94         FinishProgressView();
     95        progress.Finish();
    9596        ErrorHandling.ShowErrorDialog(this, "An error occured while stopping the task.", t.Exception);
    9697      }, TaskContinuationOptions.OnlyOnFaulted);
     
    99100
    100101    private void PauseTaskAsync() {
    101       progress = new Progress();
    102102      progress.Status = "Pausing task. Please be patient for the command to take effect.";
    103       SetProgressView(progress);
    104       Content.Pause();
    105       FinishProgressView();
     103      progress.ProgressState = ProgressState.Started;
     104      using (var view = new ProgressView(this, progress)) {
     105        Content.Pause();
     106      }
    106107    }
    107108
    108109    private void StopTaskAsync() {
    109       progress = new Progress();
    110110      progress.Status = "Stopping task. Please be patient for the command to take effect.";
    111       SetProgressView(progress);
    112       Content.Stop();
    113       FinishProgressView();
     111      progress.ProgressState = ProgressState.Started;
     112      using (var view = new ProgressView(this, progress)) {
     113        Content.Stop();
     114      }
    114115    }
    115116
    116117    private void ResumeTaskAsync() {
    117       progress = new Progress();
    118118      progress.Status = "Resuming task. Please be patient for the command to take effect.";
    119       SetProgressView(progress);
    120       Content.Restart();
    121       FinishProgressView();
    122     }
    123 
    124     private void SetProgressView(IProgress progress) {
    125       if (InvokeRequired) {
    126         Invoke(new Action<IProgress>(SetProgressView), progress);
    127       } else {
    128         progressView.Progress = progress;
    129       }
    130     }
    131 
    132     private void FinishProgressView() {
    133       if (InvokeRequired) {
    134         Invoke(new Action(FinishProgressView));
    135       } else {
    136         progress.Finish();
     119      progress.ProgressState = ProgressState.Started;
     120      using (var view = new ProgressView(this, progress)) {
     121        Content.Restart();
    137122      }
    138123    }
  • trunk/sources/HeuristicLab.Clients.Hive/3.3/HiveClient.cs

    r8159 r8165  
    351351    /// </summary>
    352352    /// <param name="parentHiveTask">shall be null if its the root task</param>
    353     private void UploadTaskWithChildren(IProgress progress, HiveTask hiveTask, HiveTask parentHiveTask, IEnumerable<Guid> groups, int[] taskCount, int totalJobCount, Guid configPluginId, Guid jobId, ILog log, bool isPrivileged, CancellationToken cancellationToken) {
     353    private void UploadTaskWithChildren(Progress progress, HiveTask hiveTask, HiveTask parentHiveTask, IEnumerable<Guid> groups, int[] taskCount, int totalJobCount, Guid configPluginId, Guid jobId, ILog log, bool isPrivileged, CancellationToken cancellationToken) {
    354354      taskUploadSemaphore.WaitOne();
    355355      bool semaphoreReleased = false;
  • trunk/sources/HeuristicLab.Clients.Hive/3.3/RefreshableJob.cs

    r8156 r8165  
    167167    }
    168168
    169     private IProgress progress;
    170     public IProgress Progress {
     169    private Progress progress;
     170    public Progress Progress {
    171171      get { return progress; }
    172172      set {
  • trunk/sources/HeuristicLab.Clients.OKB.Views/3.3/RunCreation/Views/OKBExperimentUploadView.cs

    r8160 r8165  
    3939    public new IOptimizer Content {
    4040      get { return (IOptimizer)base.Content; }
    41       set {
    42         base.Content = value;
    43       }
     41      set { base.Content = value; }
    4442    }
    4543
     
    5654    Algorithm selectedAlgorithm = null;
    5755    Problem selectedProblem = null;
    58     private ProgressView progressView;
    59     private IProgress progress;
     56    private ProgressView progressView; // this has to be disposed manually!
     57    private Progress progress;
    6058
    6159    public OKBExperimentUploadView() {
    6260      InitializeComponent();
    63       progressView = new ProgressView(this);
     61      progress = new Progress() {
     62        CanBeCanceled = false,
     63        ProgressState = ProgressState.Finished
     64      };
    6465    }
    6566
     
    9596      RunCreationClient.Instance.Refreshing += new EventHandler(RunCreationClient_Refreshing);
    9697      RunCreationClient.Instance.Refreshed += new EventHandler(RunCreationClient_Refreshed);
     98      progressView = new ProgressView(this, progress);
    9799    }
    98100
     
    100102      RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
    101103      RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
     104      if (progressView != null) {
     105        progressView.Content = null;
     106        progressView.Dispose();
     107        progressView = null;
     108      }
    102109      base.DeregisterContentEvents();
    103110    }
     
    191198        Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
    192199      } else {
    193         progress = new Progress();
    194200        progress.Status = "Refreshing algorithms and problems...";
    195         SetProgressView(progress);
     201        progress.ProgressState = ProgressState.Started;
    196202      }
    197203    }
     
    201207        Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
    202208      } else {
    203         FinishProgressView();
     209        progress.Finish();
    204210        SetEnabledStateOfControls();
    205211      }
     
    209215      var task = System.Threading.Tasks.Task.Factory.StartNew(UploadAsync);
    210216      task.ContinueWith((t) => {
    211         FinishProgressView();
     217        progress.Finish();
    212218        PluginInfrastructure.ErrorHandling.ShowErrorDialog("An exception occured while uploading the runs to the OKB.", t.Exception);
    213219      }, TaskContinuationOptions.OnlyOnFaulted);
     
    215221
    216222    private void UploadAsync() {
    217       progress = new Progress();
    218223      progress.Status = "Uploading runs to OKB...";
    219224      progress.ProgressValue = 0;
     
    221226      int i = 0;
    222227
    223       SetProgressView(progress);
    224       foreach (DataGridViewRow row in dataGridView.Rows) {
    225         selectedAlgorithm = algorithms.Where(x => x.Name == row.Cells[algorithmColumnIndex].Value.ToString()).FirstOrDefault();
    226         selectedProblem = problems.Where(x => x.Name == row.Cells[problemColumnIndex].Value.ToString()).FirstOrDefault();
    227         if (selectedAlgorithm == null || selectedProblem == null) {
    228           throw new ArgumentException("Can't retrieve the algorithm/problem to upload");
    229         }
    230 
    231         OKBRun run = new OKBRun(selectedAlgorithm.Id, selectedProblem.Id, row.Tag as IRun, UserInformation.Instance.User.Id);
    232         run.Store();
    233         i++;
    234         progress.ProgressValue = ((double)i) / count;
    235       }
    236       FinishProgressView();
     228      using (var view = new ProgressView(this, progress)) {
     229        foreach (DataGridViewRow row in dataGridView.Rows) {
     230          selectedAlgorithm = algorithms.Where(x => x.Name == row.Cells[algorithmColumnIndex].Value.ToString()).FirstOrDefault();
     231          selectedProblem = problems.Where(x => x.Name == row.Cells[problemColumnIndex].Value.ToString()).FirstOrDefault();
     232          if (selectedAlgorithm == null || selectedProblem == null) {
     233            throw new ArgumentException("Can't retrieve the algorithm/problem to upload");
     234          }
     235
     236          OKBRun run = new OKBRun(selectedAlgorithm.Id, selectedProblem.Id, row.Tag as IRun, UserInformation.Instance.User.Id);
     237          run.Store();
     238          i++;
     239          progress.ProgressValue = ((double)i) / count;
     240        }
     241      }
    237242      ClearRuns();
    238     }
    239 
    240     private void SetProgressView(IProgress progress) {
    241       if (InvokeRequired) {
    242         Invoke(new Action<IProgress>(SetProgressView), progress);
    243       } else {
    244         progressView.Progress = progress;
    245       }
    246     }
    247 
    248     private void FinishProgressView() {
    249       if (InvokeRequired) {
    250         Invoke(new Action(FinishProgressView));
    251       } else {
    252         progress.Finish();
    253       }
    254243    }
    255244
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Views/ProgressView.cs

    r8159 r8165  
    2121
    2222using System;
     23using System.ComponentModel;
    2324using System.Windows.Forms;
    2425
    2526namespace HeuristicLab.MainForm.WindowsForms {
    26   public partial class ProgressView : HeuristicLab.MainForm.WindowsForms.View {
     27  [View("ProgressView")]
     28  [Content(typeof(IProgress), true)]
     29  public partial class ProgressView : AsynchronousContentView {
     30    private const int DefaultCancelTimeoutMs = 3000;
    2731    private ContentView parentView;
    2832
    29     private IProgress progress;
    30     public IProgress Progress {
    31       get { return progress; }
    32       set {
    33         if (progress == value) return;
    34         Finish();
    35         progress = value;
    36         RegisterProgressEvents();
    37         ShowProgress();
    38         OnProgressChanged();
    39       }
    40     }
    41 
    42     private bool cancelEnabled;
    43     public bool CancelEnabled {
    44       get { return cancelEnabled; }
    45       set {
    46         cancelEnabled = value;
    47         SetCancelButtonVisibility();
    48       }
     33    [Category("Custom"), Description("The time that the process is allowed to exit.")]
     34    [DefaultValue(DefaultCancelTimeoutMs)]
     35    public int CancelTimeoutMs { get; set; }
     36    private bool ShouldSerializeCancelTimeoutMs() { return CancelTimeoutMs != DefaultCancelTimeoutMs; }
     37
     38    public new IProgress Content {
     39      get { return (IProgress)base.Content; }
     40      set { base.Content = value; }
     41    }
     42
     43    public ProgressView() {
     44      InitializeComponent();
     45    }
     46    public ProgressView(IProgress progress)
     47      : this() {
     48      Content = progress;
     49    }
     50    public ProgressView(ContentView parentView)
     51      : this() {
     52      if (parentView == null) throw new ArgumentNullException("parentView", "The parent view is null.");
     53      this.parentView = parentView;
     54    }
     55    public ProgressView(ContentView parentView, IProgress progress)
     56      : this(parentView) {
     57      Content = progress;
     58    }
     59
     60    protected override void RegisterContentEvents() {
     61      Content.StatusChanged += new EventHandler(progress_StatusChanged);
     62      Content.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
     63      Content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
     64      Content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
     65      base.RegisterContentEvents();
     66    }
     67
     68    protected override void DeregisterContentEvents() {
     69      base.DeregisterContentEvents();
     70      Content.StatusChanged -= new EventHandler(progress_StatusChanged);
     71      Content.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
     72      Content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
     73      Content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
     74    }
     75
     76    protected override void OnContentChanged() {
     77      base.OnContentChanged();
     78      if (Content == null) {
     79        HideProgress();
     80      } else {
     81        if (Content.ProgressState == ProgressState.Started)
     82          ShowProgress();
     83      }
     84    }
     85
     86    protected override void SetEnabledStateOfControls() {
     87      base.SetEnabledStateOfControls();
     88      cancelButton.Visible = Content != null && Content.CanBeCanceled;
     89      cancelButton.Enabled = Content != null && Content.CanBeCanceled && !ReadOnly;
    4990    }
    5091
    5192    private void ShowProgress() {
    52       if (progress != null) {
    53         this.Left = (parentView.ClientRectangle.Width / 2) - (this.Width / 2);
    54         this.Top = (parentView.ClientRectangle.Height / 2) - (this.Height / 2);
    55         this.Anchor = AnchorStyles.Left | AnchorStyles.Top;
    56 
    57         LockBackground();
    58 
    59         if (!parentView.Controls.Contains(this)) {
    60           parentView.Controls.Add(this);
    61         }
    62 
    63         BringToFront();
     93      if (InvokeRequired) Invoke((Action)ShowProgress);
     94      else {
     95        if (parentView != null) {
     96          this.Left = (parentView.ClientRectangle.Width / 2) - (this.Width / 2);
     97          this.Top = (parentView.ClientRectangle.Height / 2) - (this.Height / 2);
     98          this.Anchor = AnchorStyles.None;
     99
     100          LockBackground();
     101
     102          if (!parentView.Controls.Contains(this))
     103            parentView.Controls.Add(this);
     104
     105          BringToFront();
     106        }
     107        UpdateProgressValue();
     108        UpdateProgressStatus();
    64109        Visible = true;
    65110      }
    66111    }
    67112
    68     public ProgressView(ContentView parentView) {
    69       InitializeComponent();
    70       CancelEnabled = false;
    71 
    72       if (parentView != null) {
    73         this.parentView = parentView;
    74       } else {
    75         throw new ArgumentNullException("The parent view is null.");
    76       }
    77     }
    78 
    79     private void RegisterProgressEvents() {
    80       if (progress == null) return;
    81       progress.Finished += new EventHandler(progress_Finished);
    82       progress.StatusChanged += new EventHandler(progress_StatusChanged);
    83       progress.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
    84       progress.Canceled += new EventHandler(progress_Canceled);
    85     }
    86 
    87     private void DeregisterProgressEvents() {
    88       if (progress == null) return;
    89       progress.Finished -= new EventHandler(progress_Finished);
    90       progress.StatusChanged -= new EventHandler(progress_StatusChanged);
    91       progress.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
    92       progress.Canceled -= new EventHandler(progress_Canceled);
    93     }
    94 
    95     private void progress_Finished(object sender, EventArgs e) {
    96       Finish();
     113    private void HideProgress() {
     114      if (InvokeRequired) Invoke((Action)HideProgress);
     115      else {
     116        if (parentView != null) {
     117          if (parentView.Controls.Contains(this))
     118            parentView.Controls.Remove(this);
     119
     120          UnlockBackground();
     121        }
     122        Visible = false;
     123      }
    97124    }
    98125
     
    105132    }
    106133
    107     void progress_Canceled(object sender, EventArgs e) {
    108       Finish();
     134    private void Content_ProgressStateChanged(object sender, EventArgs e) {
     135      if (Content.ProgressState == ProgressState.Finished
     136        || Content.ProgressState == ProgressState.Canceled)
     137        HideProgress();
     138      if (Content.ProgressState == ProgressState.Started)
     139        ShowProgress();
     140    }
     141
     142    private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
     143      SetEnabledStateOfControls();
    109144    }
    110145
     
    115150        parentView.Locked = true;
    116151        parentView.ReadOnly = true;
    117         Enabled = true;
     152        Locked = false;
    118153        ReadOnly = false;
     154      }
     155    }
     156
     157    private void UnlockBackground() {
     158      if (InvokeRequired) Invoke((Action)UnlockBackground);
     159      else {
     160        parentView.Locked = false;
     161        parentView.ReadOnly = false;
     162        Locked = true;
     163        ReadOnly = true;
    119164      }
    120165    }
     
    123168      if (InvokeRequired) Invoke((Action)UpdateProgressValue);
    124169      else {
    125         if (progress != null) {
    126           double progressValue = progress.ProgressValue;
    127           if (progressValue < progressBar.Minimum || progressValue > progressBar.Maximum) {
    128             progressBar.Style = ProgressBarStyle.Marquee;
    129             progressBar.Value = progressBar.Minimum;
     170        if (Content != null) {
     171          double progressValue = Content.ProgressValue;
     172          if (progressValue <= 0.0 || progressValue > 1.0) {
     173            if (progressBar.Style != ProgressBarStyle.Marquee)
     174              progressBar.Style = ProgressBarStyle.Marquee;
    130175          } else {
    131             progressBar.Style = ProgressBarStyle.Blocks;
     176            if (progressBar.Style != ProgressBarStyle.Blocks)
     177              progressBar.Style = ProgressBarStyle.Blocks;
    132178            progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
    133179          }
     
    138184    private void UpdateProgressStatus() {
    139185      if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
    140       else {
    141         if (progress != null) {
    142           string status = progress.Status;
    143           statusLabel.Text = progress.Status;
    144         }
    145       }
    146     }
    147 
    148     private void Finish() {
    149       if (InvokeRequired) {
    150         Invoke(new Action(Finish));
    151       } else {
    152         progressBar.Value = progressBar.Maximum;
    153         parentView.Controls.Remove(this);
    154         parentView.Locked = false;
    155         parentView.ReadOnly = false;
    156         DeregisterProgressEvents();
    157         progress = null;
    158         Visible = false;
    159       }
     186      else if (Content != null)
     187        statusLabel.Text = Content.Status;
    160188    }
    161189
    162190    private void cancelButton_Click(object sender, EventArgs e) {
    163       if (progress != null) {
    164         progress.CancelRequested = true;
    165         cancelButton.Enabled = false;
    166       }
    167     }
    168 
    169     private void SetCancelButtonVisibility() {
    170       if (InvokeRequired) {
    171         Invoke((Action)SetCancelButtonVisibility);
    172       } else {
    173         cancelButton.Visible = cancelEnabled;
    174       }
    175     }
    176 
    177     private void OnProgressChanged() {
    178       UpdateProgressStatus();
    179       UpdateProgressValue();
     191      if (Content != null) {
     192        try {
     193          Content.Cancel(CancelTimeoutMs);
     194          ReadOnly = true;
     195          cancelButtonTimer.Interval = CancelTimeoutMs;
     196          cancelButtonTimer.Start();
     197        } catch (NotSupportedException nse) {
     198          PluginInfrastructure.ErrorHandling.ShowErrorDialog(nse);
     199        }
     200      }
     201    }
     202
     203    private void cancelButtonTimer_Tick(object sender, EventArgs e) {
     204      cancelButtonTimer.Stop();
     205      if (Visible) ReadOnly = false;
    180206    }
    181207  }
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Views/ProgressView.designer.cs

    r8111 r8165  
    11
    22namespace HeuristicLab.MainForm.WindowsForms {
    3   partial class ProgressView : View {
     3  partial class ProgressView {
    44    /// <summary>
    55    /// Required designer variable.
     
    2525    /// </summary>
    2626    private void InitializeComponent() {
     27      this.components = new System.ComponentModel.Container();
    2728      this.progressBar = new System.Windows.Forms.ProgressBar();
    2829      this.statusLabel = new System.Windows.Forms.Label();
    2930      this.cancelButton = new System.Windows.Forms.Button();
    3031      this.panel = new System.Windows.Forms.Panel();
     32      this.cancelButtonTimer = new System.Windows.Forms.Timer(this.components);
    3133      this.panel.SuspendLayout();
    3234      this.SuspendLayout();
     
    7577      this.panel.TabIndex = 3;
    7678      //
     79      // cancelButtonTimer
     80      //
     81      this.cancelButtonTimer.Tick += new System.EventHandler(this.cancelButtonTimer_Tick);
     82      //
    7783      // ProgressView
    7884      //
    79       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    8085      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
    8186      this.Controls.Add(this.panel);
     
    9398    private System.Windows.Forms.Button cancelButton;
    9499    private System.Windows.Forms.Panel panel;
     100    private System.Windows.Forms.Timer cancelButtonTimer;
    95101  }
    96102}
  • trunk/sources/HeuristicLab.MainForm/3.3/Interfaces/IProgress.cs

    r8156 r8165  
    2121
    2222using System;
     23using HeuristicLab.Common;
    2324
    2425namespace HeuristicLab.MainForm {
    25   public interface IProgress {
    26     string Status { get; set; }
    27     double ProgressValue { get; set; }
     26  public enum ProgressState { Started = 1, Canceled = 2, Finished = 3 };
    2827
    29     void Finish();
    30     void SignalSuccessfulCancelation();
     28  public interface IProgress : IContent {
     29    /// <summary>
     30    /// Gets the currently associated status text with the progress.
     31    /// </summary>
     32    string Status { get; }
     33    /// <summary>
     34    /// Gets 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.
     36    /// </summary>
     37    double ProgressValue { get; }
     38    /// <summary>
     39    /// Gets 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>
     48    bool CanBeCanceled { get; }
    3149
    32     bool CancelRequested { get; set; }
     50    /// <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.
     54    /// </summary>
     55    /// <exception cref="NotSupportedException">Thrown when cancellation is not supported.</exception>
     56    /// <param name="timeoutMs">The operation is given a certain timeout to cancel. If the operation doesn't cancel in this time it will be forcibly closed.</param>
     57    void Cancel(int timeoutMs);
    3358
    34     event EventHandler Finished;
     59    /// <summary>
     60    /// The status text changed.
     61    /// </summary>
    3562    event EventHandler StatusChanged;
     63    /// <summary>
     64    /// 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.
     65    /// </summary>
    3666    event EventHandler ProgressValueChanged;
    37     event EventHandler Canceled;
     67    /// <summary>
     68    /// The state of the progress changed. The handler is supposed to query the ProgressState property.
     69    /// </summary>
     70    event EventHandler ProgressStateChanged;
     71    /// <summary>
     72    /// The progress' ability to cancel changed.
     73    /// </summary>
     74    event EventHandler CanBeCanceledChanged;
     75    /// <summary>
     76    /// A cancelation is requested with a certain timeout (in ms) in which it should occur gracefully. If the timeout is surpassed, it should be forcibly canceled.
     77    /// </summary>
     78    event EventHandler<EventArgs<int>> CancelRequested;
    3879  }
    3980}
  • trunk/sources/HeuristicLab.MainForm/3.3/Progress.cs

    r8145 r8165  
    2121
    2222using System;
     23using HeuristicLab.Common;
    2324
    2425namespace HeuristicLab.MainForm {
     
    2627    private string status;
    2728    public string Status {
    28       get {
    29         return this.status;
    30       }
     29      get { return status; }
    3130      set {
    32         if (this.status != value) {
    33           this.status = value;
     31        if (status != value) {
     32          status = value;
    3433          OnStatusChanged();
    3534        }
     
    3736    }
    3837
    39     public bool CancelRequested { get; set; }
    40 
    4138    private double progressValue;
    4239    public double ProgressValue {
    43       get {
    44         return this.progressValue;
    45       }
     40      get { return progressValue; }
    4641      set {
    47         if (this.progressValue != value) {
    48           this.progressValue = value;
     42        if (progressValue != value) {
     43          progressValue = value;
    4944          OnProgressChanged();
    5045        }
     
    5247    }
    5348
    54     public Progress() { }
    55 
    56     public Progress(string status) {
    57       this.Status = status;
     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      }
    5858    }
    5959
    60     public void Finish() {
    61       OnFinished();
     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      }
    6269    }
    6370
    64     public void SignalSuccessfulCancelation() {
    65       OnCanceled();
     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>
     91    public void Finish() {
     92      if (ProgressValue != 1.0) ProgressValue = 1.0;
     93      ProgressState = ProgressState.Finished;
    6694    }
    6795
    6896    #region Event Handler
    69     public event EventHandler Canceled;
    70     private void OnCanceled() {
    71       var handler = Canceled;
    72       try {
    73         if (handler != null) handler(this, EventArgs.Empty);
    74       }
    75       catch (Exception) { }
    76     }
    77 
    78     public event EventHandler Finished;
    79     private void OnFinished() {
    80       var handler = Finished;
    81       try {
    82         if (handler != null) handler(this, EventArgs.Empty);
    83       }
    84       catch (Exception) { }
    85     }
    86 
    8797    public event EventHandler StatusChanged;
    8898    private void OnStatusChanged() {
     
    90100      try {
    91101        if (handler != null) handler(this, EventArgs.Empty);
    92       }
    93       catch (Exception) { }
     102      } catch { }
    94103    }
    95104
     
    99108      try {
    100109        if (handler != null) handler(this, EventArgs.Empty);
    101       }
    102       catch (Exception) { }
     110      } catch { }
     111    }
     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 { }
    103136    }
    104137    #endregion
Note: See TracChangeset for help on using the changeset viewer.