Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Core/3.3/Executable.cs @ 13349

Last change on this file since 13349 was 13349, checked in by jkarder, 8 years ago

#2258: added StartAsync to IExecutable

File size: 5.9 KB
RevLine 
[3262]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3262]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.Drawing;
[13349]24using System.Threading;
25using System.Threading.Tasks;
[3262]26using HeuristicLab.Common;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Core {
30  [Item("Executable", "A base class for executables.")]
31  [StorableClass]
32  public abstract class Executable : Item, IExecutable {
[7201]33    public static new Image StaticItemImage {
34      get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
35    }
[3262]36    public override Image ItemImage {
[3351]37      get {
[5287]38        if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePrepared;
39        else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStarted;
40        else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePaused;
41        else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStopped;
[7201]42        else return base.ItemImage;
[3351]43      }
[3262]44    }
45
46    [Storable]
47    private ExecutionState executionState;
48    public ExecutionState ExecutionState {
49      get { return executionState; }
50      private set {
51        if (executionState != value) {
52          executionState = value;
53          OnExecutionStateChanged();
[3351]54          OnItemImageChanged();
[3262]55        }
56      }
57    }
58
59    [Storable]
60    private TimeSpan executionTime;
61    public TimeSpan ExecutionTime {
62      get { return executionTime; }
63      protected set {
64        executionTime = value;
65        OnExecutionTimeChanged();
66      }
67    }
68
[4722]69    [StorableConstructor]
70    protected Executable(bool deserializing) : base(deserializing) { }
71    protected Executable(Executable original, Cloner cloner)
72      : base(original, cloner) {
73      executionState = original.executionState;
74      executionTime = original.executionTime;
75    }
[3262]76    protected Executable() {
77      executionState = ExecutionState.Stopped;
78      executionTime = TimeSpan.Zero;
79    }
80
81    public virtual void Prepare() {
82      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
83        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
84      ExecutionTime = TimeSpan.Zero;
85    }
[13349]86    public void Start() {
87      StartAsync().Wait();
88    }
89    public async Task StartAsync() {
90      await StartAsync(new CancellationToken());
91    }
92    public virtual async Task StartAsync(CancellationToken cancellationToken) {
[3262]93      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
94        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
95    }
96    public virtual void Pause() {
97      if (ExecutionState != ExecutionState.Started)
98        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
99    }
100    public virtual void Stop() {
101      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
102        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
103    }
104
105    public event EventHandler ExecutionStateChanged;
106    protected virtual void OnExecutionStateChanged() {
107      EventHandler handler = ExecutionStateChanged;
108      if (handler != null) handler(this, EventArgs.Empty);
109    }
110    public event EventHandler ExecutionTimeChanged;
111    protected virtual void OnExecutionTimeChanged() {
112      EventHandler handler = ExecutionTimeChanged;
113      if (handler != null) handler(this, EventArgs.Empty);
114    }
115    public event EventHandler Prepared;
116    protected virtual void OnPrepared() {
117      ExecutionState = ExecutionState.Prepared;
118      EventHandler handler = Prepared;
119      if (handler != null) handler(this, EventArgs.Empty);
120    }
121    public event EventHandler Started;
122    protected virtual void OnStarted() {
123      ExecutionState = ExecutionState.Started;
124      EventHandler handler = Started;
125      if (handler != null) handler(this, EventArgs.Empty);
126    }
127    public event EventHandler Paused;
128    protected virtual void OnPaused() {
129      ExecutionState = ExecutionState.Paused;
130      EventHandler handler = Paused;
131      if (handler != null) handler(this, EventArgs.Empty);
132    }
133    public event EventHandler Stopped;
134    protected virtual void OnStopped() {
135      ExecutionState = ExecutionState.Stopped;
136      EventHandler handler = Stopped;
137      if (handler != null) handler(this, EventArgs.Empty);
138    }
139    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
140    protected virtual void OnExceptionOccurred(Exception exception) {
141      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
142      if (handler != null) handler(this, new EventArgs<Exception>(exception));
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.