Changeset 10892 for stable/HeuristicLab.Scripting/3.3/CSharpScript.cs
- Timestamp:
- 05/26/14 16:41:12 (10 years ago)
- Location:
- stable
- Files:
-
- 1 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 10510-10512,10566,10577,10642,10727,10731,10747,10761,10857,10865
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Scripting/3.3/CSharpScript.cs
r10731 r10892 62 62 63 63 #region Fields & Properties 64 pr otected CSharpScriptBase CompiledScript;64 private CSharpScriptBase compiledScript; 65 65 66 66 public string Filename { get; set; } … … 82 82 public CSharpScript() { 83 83 variableStore = new VariableStore(); 84 Code = CodeTemplate; 84 85 } 85 86 public CSharpScript(string code) … … 94 95 95 96 protected virtual void RegisterScriptEvents() { 96 if ( CompiledScript != null)97 CompiledScript.ConsoleOutputChanged += CompiledScriptOnConsoleOutputChanged;97 if (compiledScript != null) 98 compiledScript.ConsoleOutputChanged += CompiledScriptOnConsoleOutputChanged; 98 99 } 99 100 100 101 protected virtual void DeregisterScriptEvents() { 101 if ( CompiledScript != null)102 CompiledScript.ConsoleOutputChanged -= CompiledScriptOnConsoleOutputChanged;102 if (compiledScript != null) 103 compiledScript.ConsoleOutputChanged -= CompiledScriptOnConsoleOutputChanged; 103 104 } 104 105 … … 106 107 107 108 public override Assembly Compile() { 108 CompiledScript = null; 109 DeregisterScriptEvents(); 110 compiledScript = null; 109 111 var assembly = base.Compile(); 110 112 var types = assembly.GetTypes(); 111 DeregisterScriptEvents(); 112 CompiledScript = (CSharpScriptBase)Activator.CreateInstance(types.First(x => typeof(CSharpScriptBase).IsAssignableFrom(x))); 113 compiledScript = (CSharpScriptBase)Activator.CreateInstance(types.Single(x => typeof(CSharpScriptBase).IsAssignableFrom(x))); 113 114 RegisterScriptEvents(); 114 115 return assembly; … … 116 117 #endregion 117 118 118 pr otected Thread ScriptThread;119 private Thread scriptThread; 119 120 public virtual void Execute() { 120 if (CompiledScript == null) return; 121 var executeMethod = typeof(CSharpScriptBase).GetMethod(ExecuteMethodName, BindingFlags.NonPublic | BindingFlags.Instance); 122 if (executeMethod != null) { 123 ScriptThread = new Thread(() => { 124 Exception ex = null; 125 try { 126 OnScriptExecutionStarted(); 127 executeMethod.Invoke(CompiledScript, new object[] { VariableStore }); 128 } catch (ThreadAbortException) { 129 // the execution was cancelled by the user 130 } catch (TargetInvocationException e) { 131 ex = e.InnerException; 132 } finally { 133 OnScriptExecutionFinished(ex); 134 } 135 }); 136 ScriptThread.Start(); 137 } 121 if (compiledScript == null) return; 122 scriptThread = new Thread(() => { 123 Exception ex = null; 124 try { 125 OnScriptExecutionStarted(); 126 compiledScript.Execute(VariableStore); 127 } catch (ThreadAbortException) { 128 // the execution was cancelled by the user 129 } catch (Exception e) { 130 ex = e; 131 } finally { 132 OnScriptExecutionFinished(ex); 133 } 134 }); 135 scriptThread.Start(); 138 136 } 139 137 140 138 public virtual void Kill() { 141 if ( ScriptThread.IsAlive)142 ScriptThread.Abort();139 if (scriptThread.IsAlive) 140 scriptThread.Abort(); 143 141 } 144 142
Note: See TracChangeset
for help on using the changeset viewer.