Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Core/3.3/Executable.cs @ 17886

Last change on this file since 17886 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Drawing;
24using System.Threading;
25using System.Threading.Tasks;
26using HeuristicLab.Common;
27using HEAL.Attic;
28
29namespace HeuristicLab.Core {
30  [Item("Executable", "A base class for executables.")]
31  [StorableType("17DFB815-4CF8-49EB-BC4A-98059064DB31")]
32  public abstract class Executable : Item, IExecutable {
33    public static new Image StaticItemImage {
34      get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
35    }
36    public override Image ItemImage {
37      get {
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;
42        else return base.ItemImage;
43      }
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();
54          OnItemImageChanged();
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
69    [StorableConstructor]
70    protected Executable(StorableConstructorFlag _) : base(_) { }
71    protected Executable(Executable original, Cloner cloner)
72      : base(original, cloner) {
73      executionState = original.executionState;
74      executionTime = original.executionTime;
75    }
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() {
87      Start(CancellationToken.None);
88    }
89    public virtual void Start(CancellationToken cancellationToken) {
90      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
91        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
92    }
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    }
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.