Changeset 13080 for trunk/sources/HeuristicLab.Scripting/3.3/Scripts
- Timestamp:
- 10/29/15 12:50:06 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Scripting/3.3/Scripts/CSharp/CSharpScript.cs
r13024 r13080 23 23 using System.Linq; 24 24 using System.Reflection; 25 using System.Threading;26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Core; … … 32 31 [Creatable(CreatableAttribute.Categories.Scripts, Priority = 100)] 33 32 [StorableClass] 34 public class CSharpScript : Script, IStorableContent {33 public class CSharpScript : ExecutableScript, IStorableContent { 35 34 #region Constants 36 protected const string ExecuteMethodName = "Execute";37 35 protected override string CodeTemplate { get { return ScriptTemplates.CSharpScriptTemplate; } } 38 36 #endregion … … 40 38 #region Fields & Properties 41 39 private CSharpScriptBase compiledScript; 42 private Thread scriptThread;43 private DateTime lastUpdateTime;44 40 45 41 public string Filename { get; set; } … … 50 46 get { return variableStore; } 51 47 } 52 53 [Storable]54 private TimeSpan executionTime;55 public TimeSpan ExecutionTime {56 get { return executionTime; }57 protected set {58 executionTime = value;59 OnExecutionTimeChanged();60 }61 }62 48 #endregion 63 49 64 #region Construction & Initialization50 #region Construction & Cloning 65 51 [StorableConstructor] 66 52 protected CSharpScript(bool deserializing) : base(deserializing) { } … … 68 54 : base(original, cloner) { 69 55 variableStore = cloner.Clone(original.variableStore); 70 executionTime = original.executionTime;71 56 } 72 public CSharpScript() { 57 public CSharpScript() 58 : base() { 73 59 variableStore = new VariableStore(); 74 executionTime = TimeSpan.Zero;75 Code = CodeTemplate;76 60 } 77 61 public CSharpScript(string code) 78 62 : base(code) { 79 63 variableStore = new VariableStore(); 80 executionTime = TimeSpan.Zero;81 64 } 82 65 … … 97 80 98 81 #region Compilation 99 100 82 public override Assembly Compile() { 101 83 DeregisterScriptEvents(); … … 109 91 #endregion 110 92 111 p ublic virtual void ExecuteAsync() {93 protected override void ExecuteCode() { 112 94 if (compiledScript == null) return; 113 executionTime = TimeSpan.Zero;114 scriptThread = new Thread(() => {115 Exception ex = null;116 var timer = new System.Timers.Timer(250) { AutoReset = true };117 timer.Elapsed += timer_Elapsed;118 try {119 OnScriptExecutionStarted();120 lastUpdateTime = DateTime.UtcNow;121 timer.Start();122 compiledScript.Execute(VariableStore);123 } catch (Exception e) {124 ex = e;125 } finally {126 scriptThread = null;127 timer.Elapsed -= timer_Elapsed;128 timer.Stop();129 ExecutionTime += DateTime.UtcNow - lastUpdateTime;130 OnScriptExecutionFinished(ex);131 }132 });133 scriptThread.SetApartmentState(ApartmentState.STA);134 scriptThread.Start();135 }136 95 137 public virtual void Kill() { 138 if (scriptThread == null) return; 139 scriptThread.Abort(); 140 } 141 142 private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { 143 var timer = (System.Timers.Timer)sender; 144 timer.Enabled = false; 145 DateTime now = DateTime.UtcNow; 146 ExecutionTime += now - lastUpdateTime; 147 lastUpdateTime = now; 148 timer.Enabled = true; 96 compiledScript.Execute(VariableStore); 149 97 } 150 98 151 99 protected virtual void CompiledScriptOnConsoleOutputChanged(object sender, EventArgs<string> e) { 152 100 OnConsoleOutputChanged(e.Value); 153 }154 155 public event EventHandler ScriptExecutionStarted;156 protected virtual void OnScriptExecutionStarted() {157 var handler = ScriptExecutionStarted;158 if (handler != null) handler(this, EventArgs.Empty);159 }160 161 public event EventHandler<EventArgs<Exception>> ScriptExecutionFinished;162 protected virtual void OnScriptExecutionFinished(Exception e) {163 var handler = ScriptExecutionFinished;164 if (handler != null) handler(this, new EventArgs<Exception>(e));165 101 } 166 102 … … 170 106 if (handler != null) handler(this, new EventArgs<string>(args)); 171 107 } 172 173 public event EventHandler ExecutionTimeChanged;174 protected virtual void OnExecutionTimeChanged() {175 var handler = ExecutionTimeChanged;176 if (handler != null) handler(this, EventArgs.Empty);177 }178 108 } 179 109 }
Note: See TracChangeset
for help on using the changeset viewer.