Changeset 13024 for trunk/sources/HeuristicLab.Scripting/3.3/Scripts/CSharp
- Timestamp:
- 10/16/15 13:31:14 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Scripting/3.3/Scripts/CSharp/CSharpScript.cs
r12504 r13024 40 40 #region Fields & Properties 41 41 private CSharpScriptBase compiledScript; 42 private Thread scriptThread; 43 private DateTime lastUpdateTime; 42 44 43 45 public string Filename { get; set; } … … 47 49 public VariableStore VariableStore { 48 50 get { return variableStore; } 51 } 52 53 [Storable] 54 private TimeSpan executionTime; 55 public TimeSpan ExecutionTime { 56 get { return executionTime; } 57 protected set { 58 executionTime = value; 59 OnExecutionTimeChanged(); 60 } 49 61 } 50 62 #endregion … … 56 68 : base(original, cloner) { 57 69 variableStore = cloner.Clone(original.variableStore); 70 executionTime = original.executionTime; 58 71 } 59 72 public CSharpScript() { 60 73 variableStore = new VariableStore(); 74 executionTime = TimeSpan.Zero; 61 75 Code = CodeTemplate; 62 76 } … … 64 78 : base(code) { 65 79 variableStore = new VariableStore(); 80 executionTime = TimeSpan.Zero; 66 81 } 67 82 … … 94 109 #endregion 95 110 96 private Thread scriptThread;97 111 public virtual void ExecuteAsync() { 98 112 if (compiledScript == null) return; 113 executionTime = TimeSpan.Zero; 99 114 scriptThread = new Thread(() => { 100 115 Exception ex = null; 116 var timer = new System.Timers.Timer(250) { AutoReset = true }; 117 timer.Elapsed += timer_Elapsed; 101 118 try { 102 119 OnScriptExecutionStarted(); 120 lastUpdateTime = DateTime.UtcNow; 121 timer.Start(); 103 122 compiledScript.Execute(VariableStore); 104 } 105 catch (Exception e) { 123 } catch (Exception e) { 106 124 ex = e; 107 } 108 finally { 125 } finally { 109 126 scriptThread = null; 127 timer.Elapsed -= timer_Elapsed; 128 timer.Stop(); 129 ExecutionTime += DateTime.UtcNow - lastUpdateTime; 110 130 OnScriptExecutionFinished(ex); 111 131 } … … 118 138 if (scriptThread == null) return; 119 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; 120 149 } 121 150 … … 141 170 if (handler != null) handler(this, new EventArgs<string>(args)); 142 171 } 172 173 public event EventHandler ExecutionTimeChanged; 174 protected virtual void OnExecutionTimeChanged() { 175 var handler = ExecutionTimeChanged; 176 if (handler != null) handler(this, EventArgs.Empty); 177 } 143 178 } 144 179 }
Note: See TracChangeset
for help on using the changeset viewer.