Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Hive.Views/3.3/HiveTasks/OptimizerHiveTaskView.cs @ 7225

Last change on this file since 7225 was 7156, checked in by ascheibe, 13 years ago

#1672 fixed starting, pausing and stopping of jobs and tasks

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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;
23using System.Windows.Forms;
24using HeuristicLab.MainForm;
25using System.Threading.Tasks;
26using HeuristicLab.PluginInfrastructure;
27
28namespace HeuristicLab.Clients.Hive.Views {
29  [View("OptimizerHiveTask View")]
30  [Content(typeof(OptimizerHiveTask), true)]
31  public partial class OptimizerHiveTaskView : HiveTaskView {
32    private ProgressView progressView;
33
34    public new OptimizerHiveTask Content {
35      get { return (OptimizerHiveTask)base.Content; }
36      set {
37        if (base.Content != value) {
38          base.Content = value;
39        }
40      }
41    }
42
43    public OptimizerHiveTaskView() {
44      InitializeComponent();
45    }
46
47    protected override void Job_ItemChanged(object sender, EventArgs e) {
48      if (Content != null && Content.Task != null && Content.ItemTask.Item != null) {
49        runCollectionViewHost.Content = Content.ItemTask.Item.Runs;
50      } else {
51        runCollectionViewHost.Content = null;
52      }
53    }
54    #region Content Events
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
57      Content.IsControllableChanged += new EventHandler(Content_IsControllableChanged);
58    }
59
60    protected override void DeregisterContentEvents() {
61      Content.IsControllableChanged -= new EventHandler(Content_IsControllableChanged);
62      base.DeregisterContentEvents();
63    }
64
65    protected virtual void Content_IsControllableChanged(object sender, EventArgs e) {
66      SetEnabledStateOfControls();
67    }
68
69    #endregion
70
71    #region Child Control Events
72    private void restartButton_Click(object sender, EventArgs e) {
73      var task = System.Threading.Tasks.Task.Factory.StartNew(ResumeTaskAsync);
74      task.ContinueWith((t) => {
75        FinishProgressView();
76        ErrorHandling.ShowErrorDialog(this, "An error occured while resuming the task.", t.Exception);
77      }, TaskContinuationOptions.OnlyOnFaulted);
78    }
79
80    private void pauseButton_Click(object sender, EventArgs e) {
81      var task = System.Threading.Tasks.Task.Factory.StartNew(PauseTaskAsync);
82      task.ContinueWith((t) => {
83        FinishProgressView();
84        ErrorHandling.ShowErrorDialog(this, "An error occured while pausing the task.", t.Exception);
85      }, TaskContinuationOptions.OnlyOnFaulted);
86    }
87
88    private void stopButton_Click(object sender, EventArgs e) {
89      var task = System.Threading.Tasks.Task.Factory.StartNew(StopTaskAsync);
90      task.ContinueWith((t) => {
91        FinishProgressView();
92        ErrorHandling.ShowErrorDialog(this, "An error occured while stopping the task.", t.Exception);
93      }, TaskContinuationOptions.OnlyOnFaulted);
94    }
95    #endregion
96
97    private void PauseTaskAsync() {
98      IProgress prog = new Progress();
99      prog.Status = "Pausing task. Please be patient for the command to take effect.";
100      SetProgressView(prog);
101      Content.Pause();
102      FinishProgressView();
103    }
104
105    private void StopTaskAsync() {
106      IProgress prog = new Progress();
107      prog.Status = "Stopping task. Please be patient for the command to take effect.";
108      SetProgressView(prog);
109      Content.Stop();
110      FinishProgressView();
111    }
112
113    private void ResumeTaskAsync() {
114      IProgress prog = new Progress();
115      prog.Status = "Resuming task. Please be patient for the command to take effect.";
116      SetProgressView(prog);
117      Content.Restart();
118      FinishProgressView();
119    }
120
121    private void SetProgressView(IProgress progress) {
122      if (InvokeRequired) {
123        Invoke(new Action<IProgress>(SetProgressView), progress);
124      } else {
125        if (progressView == null) {
126          progressView = new ProgressView(this, progress);
127        } else {
128          progressView.Progress = progress;
129        }
130      }
131    }
132
133    private void FinishProgressView() {
134      if (InvokeRequired) {
135        Invoke(new Action(FinishProgressView));
136      } else {
137        if (progressView != null) {
138          progressView.Finish();
139          progressView = null;
140          SetEnabledStateOfControls();
141        }
142      }
143    }
144
145    protected override void SetEnabledStateOfControls() {
146      base.SetEnabledStateOfControls();
147
148      this.restartButton.Enabled = Content != null && Content.IsControllable && !Content.Task.Command.HasValue && (Content.Task.State == TaskState.Paused || Content.Task.State == TaskState.Failed || Content.Task.State == TaskState.Aborted);
149      this.pauseButton.Enabled = Content != null && Content.IsControllable && !Content.Task.Command.HasValue && Content.Task.State == TaskState.Calculating;
150      this.stopButton.Enabled = Content != null && Content.IsControllable && !Content.Task.Command.HasValue && (Content.Task.State == TaskState.Calculating || Content.Task.State == TaskState.Waiting || Content.Task.State == TaskState.Paused);
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.