Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Core/3.3/Executable.cs @ 16189

Last change on this file since 16189 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 6.0 KB
RevLine 
[3262]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 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;
[15292]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    }
86    public virtual void Start() {
[15292]87      Start(CancellationToken.None);
88    }
89    public virtual void Start(CancellationToken cancellationToken) {
[3262]90      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
91        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
92    }
[15292]93    public virtual async Task StartAsync() { await StartAsync(CancellationToken.None); }
94    public virtual async Task StartAsync(CancellationToken cancellationToken) {
95      await AsyncHelper.DoAsync(Start, cancellationToken);
96    }
[3262]97    public virtual void Pause() {
98      if (ExecutionState != ExecutionState.Started)
99        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
100    }
101    public virtual void Stop() {
102      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
103        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
104    }
105
106    public event EventHandler ExecutionStateChanged;
107    protected virtual void OnExecutionStateChanged() {
108      EventHandler handler = ExecutionStateChanged;
109      if (handler != null) handler(this, EventArgs.Empty);
110    }
111    public event EventHandler ExecutionTimeChanged;
112    protected virtual void OnExecutionTimeChanged() {
113      EventHandler handler = ExecutionTimeChanged;
114      if (handler != null) handler(this, EventArgs.Empty);
115    }
116    public event EventHandler Prepared;
117    protected virtual void OnPrepared() {
118      ExecutionState = ExecutionState.Prepared;
119      EventHandler handler = Prepared;
120      if (handler != null) handler(this, EventArgs.Empty);
121    }
122    public event EventHandler Started;
123    protected virtual void OnStarted() {
124      ExecutionState = ExecutionState.Started;
125      EventHandler handler = Started;
126      if (handler != null) handler(this, EventArgs.Empty);
127    }
128    public event EventHandler Paused;
129    protected virtual void OnPaused() {
130      ExecutionState = ExecutionState.Paused;
131      EventHandler handler = Paused;
132      if (handler != null) handler(this, EventArgs.Empty);
133    }
134    public event EventHandler Stopped;
135    protected virtual void OnStopped() {
136      ExecutionState = ExecutionState.Stopped;
137      EventHandler handler = Stopped;
138      if (handler != null) handler(this, EventArgs.Empty);
139    }
140    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
141    protected virtual void OnExceptionOccurred(Exception exception) {
142      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
143      if (handler != null) handler(this, new EventArgs<Exception>(exception));
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.