- Timestamp:
- 07/08/16 14:40:02 (8 years ago)
- Location:
- branches/crossvalidation-2434
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/crossvalidation-2434
- Property svn:mergeinfo changed
-
branches/crossvalidation-2434/HeuristicLab.Scripting/3.3/Scripts/CSharp/CSharpScript.cs
r12504 r14029 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 { 35 #region Constants 36 protected const string ExecuteMethodName = "Execute"; 37 protected override string CodeTemplate { get { return ScriptTemplates.CSharpScriptTemplate; } } 38 #endregion 39 33 public class CSharpScript : ExecutableScript, IStorableContent { 40 34 #region Fields & Properties 41 35 private CSharpScriptBase compiledScript; … … 50 44 #endregion 51 45 52 #region Construction & Initialization46 #region Construction & Cloning 53 47 [StorableConstructor] 54 48 protected CSharpScript(bool deserializing) : base(deserializing) { } … … 57 51 variableStore = cloner.Clone(original.variableStore); 58 52 } 59 public CSharpScript() { 53 public CSharpScript() 54 : base(ScriptTemplates.CSharpScriptTemplate) { 60 55 variableStore = new VariableStore(); 61 Code = CodeTemplate;62 56 } 63 57 public CSharpScript(string code) … … 82 76 83 77 #region Compilation 84 85 78 public override Assembly Compile() { 86 79 DeregisterScriptEvents(); … … 94 87 #endregion 95 88 96 private Thread scriptThread; 97 public virtual void ExecuteAsync() { 89 protected override void ExecuteCode() { 98 90 if (compiledScript == null) return; 99 scriptThread = new Thread(() => {100 Exception ex = null;101 try {102 OnScriptExecutionStarted();103 compiledScript.Execute(VariableStore);104 }105 catch (Exception e) {106 ex = e;107 }108 finally {109 scriptThread = null;110 OnScriptExecutionFinished(ex);111 }112 });113 scriptThread.SetApartmentState(ApartmentState.STA);114 scriptThread.Start();115 }116 91 117 public virtual void Kill() { 118 if (scriptThread == null) return; 119 scriptThread.Abort(); 92 compiledScript.Execute(VariableStore); 120 93 } 121 94 122 95 protected virtual void CompiledScriptOnConsoleOutputChanged(object sender, EventArgs<string> e) { 123 96 OnConsoleOutputChanged(e.Value); 124 }125 126 public event EventHandler ScriptExecutionStarted;127 protected virtual void OnScriptExecutionStarted() {128 var handler = ScriptExecutionStarted;129 if (handler != null) handler(this, EventArgs.Empty);130 }131 132 public event EventHandler<EventArgs<Exception>> ScriptExecutionFinished;133 protected virtual void OnScriptExecutionFinished(Exception e) {134 var handler = ScriptExecutionFinished;135 if (handler != null) handler(this, new EventArgs<Exception>(e));136 97 } 137 98
Note: See TracChangeset
for help on using the changeset viewer.