Changeset 10857


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

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

Location:
trunk/sources
Files:
4 edited

Legend:

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

    r10731 r10857  
    2121
    2222using System;
    23 using System.Drawing;
    2423using System.Windows.Forms;
    2524using HeuristicLab.Common;
     
    9594      base.OnContentChanged();
    9695      if (Content == null) {
    97         codeEditor.UserCode = string.Empty;
    9896        variableStoreView.Content = null;
    9997      } else {
    100         codeEditor.UserCode = Content.Code;
    101         foreach (var asm in Content.GetAssemblies())
    102           codeEditor.AddAssembly(asm);
    10398        variableStoreView.Content = Content.VariableStore;
    104         if (Content.CompileErrors == null) {
    105           compilationLabel.ForeColor = SystemColors.ControlDarkDark;
    106           compilationLabel.Text = "Not compiled";
    107         } else if (Content.CompileErrors.HasErrors) {
    108           compilationLabel.ForeColor = Color.DarkRed;
    109           compilationLabel.Text = "Compilation failed";
    110         } else {
    111           compilationLabel.ForeColor = Color.DarkGreen;
    112           compilationLabel.Text = "Compilation successful";
    113         }
    11499      }
    115100    }
     
    117102    protected override void SetEnabledStateOfControls() {
    118103      base.SetEnabledStateOfControls();
    119       compileButton.Enabled = Content != null && !Locked && !ReadOnly;
    120104      startStopButton.Enabled = Content != null && (!Locked || Running);
    121       codeEditor.Enabled = Content != null && !Locked && !ReadOnly;
    122105    }
    123106
  • 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
  • trunk/sources/HeuristicLab.Scripting/3.3/CSharpScriptBase.cs

    r10731 r10857  
    5252
    5353    protected class EventWriter : TextWriter {
    54       private readonly CSharpScriptBase usb;
     54      private readonly CSharpScriptBase script;
    5555
    56       public EventWriter(CSharpScriptBase usb) {
    57         this.usb = usb;
     56      public EventWriter(CSharpScriptBase script) {
     57        this.script = script;
    5858      }
    5959
     
    6464      #region Write/WriteLine Overrides
    6565      #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()); }
    8383      #endregion
    8484
    8585      #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); }
    104104      #endregion
    105105      #endregion
  • trunk/sources/HeuristicLab.Scripting/3.3/Script.cs

    r10731 r10857  
    4949    [Storable]
    5050    private string code;
    51     public virtual string Code {
     51    public string Code {
    5252      get { return code; }
    5353      set {
     
    5858    }
    5959
    60     private string compilationUnitCode;
    61     public virtual string CompilationUnitCode {
    62       get { return compilationUnitCode; }
    63     }
    64 
    6560    private CompilerErrorCollection compileErrors;
    66     public virtual CompilerErrorCollection CompileErrors {
     61    public CompilerErrorCollection CompileErrors {
    6762      get { return compileErrors; }
    6863      private set {
     
    7974      : base(original, cloner) {
    8075      code = original.code;
    81       compilationUnitCode = original.compilationUnitCode;
    8276      if (original.compileErrors != null)
    8377        compileErrors = new CompilerErrorCollection(original.compileErrors);
    8478    }
    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;
    8982    }
    9083    public Script(string code)
     
    128121          IndentString = "  ",
    129122        });
    130       compilationUnitCode = writer.ToString();
    131123      return CodeProvider.CompileAssemblyFromDom(parameters, unit);
    132124    }
Note: See TracChangeset for help on using the changeset viewer.