Changeset 10857 for trunk/sources/HeuristicLab.Scripting
- Timestamp:
- 05/19/14 09:09:39 (11 years ago)
- Location:
- trunk/sources/HeuristicLab.Scripting/3.3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Scripting/3.3/CSharpScript.cs
r10731 r10857 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 -
trunk/sources/HeuristicLab.Scripting/3.3/CSharpScriptBase.cs
r10731 r10857 52 52 53 53 protected class EventWriter : TextWriter { 54 private readonly CSharpScriptBase usb;54 private readonly CSharpScriptBase script; 55 55 56 public EventWriter(CSharpScriptBase usb) {57 this. usb = usb;56 public EventWriter(CSharpScriptBase script) { 57 this.script = script; 58 58 } 59 59 … … 64 64 #region Write/WriteLine Overrides 65 65 #region Write 66 public override void Write(bool value) { usb.OnConsoleOutputChanged(value.ToString()); }67 public override void Write(char value) { usb.OnConsoleOutputChanged(value.ToString()); }68 public override void Write(char[] buffer) { usb.OnConsoleOutputChanged(new string(buffer)); }69 public override void Write(char[] buffer, int index, int count) { usb.OnConsoleOutputChanged(new string(buffer, index, count)); }70 public override void Write(decimal value) { usb.OnConsoleOutputChanged(value.ToString()); }71 public override void Write(double value) { usb.OnConsoleOutputChanged(value.ToString()); }72 public override void Write(float value) { usb.OnConsoleOutputChanged(value.ToString()); }73 public override void Write(int value) { usb.OnConsoleOutputChanged(value.ToString()); }74 public override void Write(long value) { usb.OnConsoleOutputChanged(value.ToString()); }75 public override void Write(object value) { usb.OnConsoleOutputChanged(value.ToString()); }76 public override void Write(string value) { usb.OnConsoleOutputChanged(value); }77 public override void Write(string format, object arg0) { usb.OnConsoleOutputChanged(string.Format(format, arg0)); }78 public override void Write(string format, object arg0, object arg1) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg0)); }79 public override void Write(string format, object arg0, object arg1, object arg2) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2)); }80 public override void Write(string format, params object[] arg) { usb.OnConsoleOutputChanged(string.Format(format, arg)); }81 public override void Write(uint value) { usb.OnConsoleOutputChanged(value.ToString()); }82 public override void Write(ulong value) { usb.OnConsoleOutputChanged(value.ToString()); }66 public override void Write(bool value) { script.OnConsoleOutputChanged(value.ToString()); } 67 public override void Write(char value) { script.OnConsoleOutputChanged(value.ToString()); } 68 public override void Write(char[] buffer) { script.OnConsoleOutputChanged(new string(buffer)); } 69 public override void Write(char[] buffer, int index, int count) { script.OnConsoleOutputChanged(new string(buffer, index, count)); } 70 public override void Write(decimal value) { script.OnConsoleOutputChanged(value.ToString()); } 71 public override void Write(double value) { script.OnConsoleOutputChanged(value.ToString()); } 72 public override void Write(float value) { script.OnConsoleOutputChanged(value.ToString()); } 73 public override void Write(int value) { script.OnConsoleOutputChanged(value.ToString()); } 74 public override void Write(long value) { script.OnConsoleOutputChanged(value.ToString()); } 75 public override void Write(object value) { script.OnConsoleOutputChanged(value.ToString()); } 76 public override void Write(string value) { script.OnConsoleOutputChanged(value); } 77 public override void Write(string format, object arg0) { script.OnConsoleOutputChanged(string.Format(format, arg0)); } 78 public override void Write(string format, object arg0, object arg1) { script.OnConsoleOutputChanged(string.Format(format, arg0, arg0)); } 79 public override void Write(string format, object arg0, object arg1, object arg2) { script.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2)); } 80 public override void Write(string format, params object[] arg) { script.OnConsoleOutputChanged(string.Format(format, arg)); } 81 public override void Write(uint value) { script.OnConsoleOutputChanged(value.ToString()); } 82 public override void Write(ulong value) { script.OnConsoleOutputChanged(value.ToString()); } 83 83 #endregion 84 84 85 85 #region WriteLine 86 public override void WriteLine() { usb.OnConsoleOutputChanged(Environment.NewLine); }87 public override void WriteLine(bool value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }88 public override void WriteLine(char value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }89 public override void WriteLine(char[] buffer) { usb.OnConsoleOutputChanged(new string(buffer) + Environment.NewLine); }90 public override void WriteLine(char[] buffer, int index, int count) { usb.OnConsoleOutputChanged(new string(buffer, index, count) + Environment.NewLine); }91 public override void WriteLine(decimal value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }92 public override void WriteLine(double value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }93 public override void WriteLine(float value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }94 public override void WriteLine(int value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }95 public override void WriteLine(long value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }96 public override void WriteLine(object value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }97 public override void WriteLine(string value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }98 public override void WriteLine(string format, object arg0) { usb.OnConsoleOutputChanged(string.Format(format, arg0) + Environment.NewLine); }99 public override void WriteLine(string format, object arg0, object arg1) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1) + Environment.NewLine); }100 public override void WriteLine(string format, object arg0, object arg1, object arg2) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2) + Environment.NewLine); }101 public override void WriteLine(string format, params object[] arg) { usb.OnConsoleOutputChanged(string.Format(format, arg) + Environment.NewLine); }102 public override void WriteLine(uint value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }103 public override void WriteLine(ulong value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }86 public override void WriteLine() { script.OnConsoleOutputChanged(Environment.NewLine); } 87 public override void WriteLine(bool value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 88 public override void WriteLine(char value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 89 public override void WriteLine(char[] buffer) { script.OnConsoleOutputChanged(new string(buffer) + Environment.NewLine); } 90 public override void WriteLine(char[] buffer, int index, int count) { script.OnConsoleOutputChanged(new string(buffer, index, count) + Environment.NewLine); } 91 public override void WriteLine(decimal value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 92 public override void WriteLine(double value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 93 public override void WriteLine(float value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 94 public override void WriteLine(int value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 95 public override void WriteLine(long value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 96 public override void WriteLine(object value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 97 public override void WriteLine(string value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 98 public override void WriteLine(string format, object arg0) { script.OnConsoleOutputChanged(string.Format(format, arg0) + Environment.NewLine); } 99 public override void WriteLine(string format, object arg0, object arg1) { script.OnConsoleOutputChanged(string.Format(format, arg0, arg1) + Environment.NewLine); } 100 public override void WriteLine(string format, object arg0, object arg1, object arg2) { script.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2) + Environment.NewLine); } 101 public override void WriteLine(string format, params object[] arg) { script.OnConsoleOutputChanged(string.Format(format, arg) + Environment.NewLine); } 102 public override void WriteLine(uint value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 103 public override void WriteLine(ulong value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 104 104 #endregion 105 105 #endregion -
trunk/sources/HeuristicLab.Scripting/3.3/Script.cs
r10731 r10857 49 49 [Storable] 50 50 private string code; 51 public virtualstring Code {51 public string Code { 52 52 get { return code; } 53 53 set { … … 58 58 } 59 59 60 private string compilationUnitCode;61 public virtual string CompilationUnitCode {62 get { return compilationUnitCode; }63 }64 65 60 private CompilerErrorCollection compileErrors; 66 public virtualCompilerErrorCollection CompileErrors {61 public CompilerErrorCollection CompileErrors { 67 62 get { return compileErrors; } 68 63 private set { … … 79 74 : base(original, cloner) { 80 75 code = original.code; 81 compilationUnitCode = original.compilationUnitCode;82 76 if (original.compileErrors != null) 83 77 compileErrors = new CompilerErrorCollection(original.compileErrors); 84 78 } 85 public Script() { 86 name = ItemName; 87 description = ItemDescription; 88 code = CodeTemplate; 79 public Script() 80 : base("Script", "An empty script.") { 81 code = string.Empty; 89 82 } 90 83 public Script(string code) … … 128 121 IndentString = " ", 129 122 }); 130 compilationUnitCode = writer.ToString();131 123 return CodeProvider.CompileAssemblyFromDom(parameters, unit); 132 124 }
Note: See TracChangeset
for help on using the changeset viewer.