Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/19/14 09:09:39 (10 years ago)
Author:
jkarder
Message:

#2136: implemented review comments from mkommend in comment:32:ticket:2136

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Scripting/3.3/CSharpScript.cs

    r10731 r10857  
    6262
    6363    #region Fields & Properties
    64     protected CSharpScriptBase CompiledScript;
     64    private CSharpScriptBase compiledScript;
    6565
    6666    public string Filename { get; set; }
     
    8282    public CSharpScript() {
    8383      variableStore = new VariableStore();
     84      Code = CodeTemplate;
    8485    }
    8586    public CSharpScript(string code)
     
    9495
    9596    protected virtual void RegisterScriptEvents() {
    96       if (CompiledScript != null)
    97         CompiledScript.ConsoleOutputChanged += CompiledScriptOnConsoleOutputChanged;
     97      if (compiledScript != null)
     98        compiledScript.ConsoleOutputChanged += CompiledScriptOnConsoleOutputChanged;
    9899    }
    99100
    100101    protected virtual void DeregisterScriptEvents() {
    101       if (CompiledScript != null)
    102         CompiledScript.ConsoleOutputChanged -= CompiledScriptOnConsoleOutputChanged;
     102      if (compiledScript != null)
     103        compiledScript.ConsoleOutputChanged -= CompiledScriptOnConsoleOutputChanged;
    103104    }
    104105
     
    106107
    107108    public override Assembly Compile() {
    108       CompiledScript = null;
     109      DeregisterScriptEvents();
     110      compiledScript = null;
    109111      var assembly = base.Compile();
    110112      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)));
    113114      RegisterScriptEvents();
    114115      return assembly;
     
    116117    #endregion
    117118
    118     protected Thread ScriptThread;
     119    private Thread scriptThread;
    119120    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();
    138136    }
    139137
    140138    public virtual void Kill() {
    141       if (ScriptThread.IsAlive)
    142         ScriptThread.Abort();
     139      if (scriptThread.IsAlive)
     140        scriptThread.Abort();
    143141    }
    144142
Note: See TracChangeset for help on using the changeset viewer.