Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/29/15 12:50:06 (9 years ago)
Author:
jkarder
Message:

#2298: added ExecutableScript and ExecutableScriptView

File:
1 edited

Legend:

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

    r13024 r13080  
    2323using System.Linq;
    2424using System.Reflection;
    25 using System.Threading;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Core;
     
    3231  [Creatable(CreatableAttribute.Categories.Scripts, Priority = 100)]
    3332  [StorableClass]
    34   public class CSharpScript : Script, IStorableContent {
     33  public class CSharpScript : ExecutableScript, IStorableContent {
    3534    #region Constants
    36     protected const string ExecuteMethodName = "Execute";
    3735    protected override string CodeTemplate { get { return ScriptTemplates.CSharpScriptTemplate; } }
    3836    #endregion
     
    4038    #region Fields & Properties
    4139    private CSharpScriptBase compiledScript;
    42     private Thread scriptThread;
    43     private DateTime lastUpdateTime;
    4440
    4541    public string Filename { get; set; }
     
    5046      get { return variableStore; }
    5147    }
    52 
    53     [Storable]
    54     private TimeSpan executionTime;
    55     public TimeSpan ExecutionTime {
    56       get { return executionTime; }
    57       protected set {
    58         executionTime = value;
    59         OnExecutionTimeChanged();
    60       }
    61     }
    6248    #endregion
    6349
    64     #region Construction & Initialization
     50    #region Construction & Cloning
    6551    [StorableConstructor]
    6652    protected CSharpScript(bool deserializing) : base(deserializing) { }
     
    6854      : base(original, cloner) {
    6955      variableStore = cloner.Clone(original.variableStore);
    70       executionTime = original.executionTime;
    7156    }
    72     public CSharpScript() {
     57    public CSharpScript()
     58      : base() {
    7359      variableStore = new VariableStore();
    74       executionTime = TimeSpan.Zero;
    75       Code = CodeTemplate;
    7660    }
    7761    public CSharpScript(string code)
    7862      : base(code) {
    7963      variableStore = new VariableStore();
    80       executionTime = TimeSpan.Zero;
    8164    }
    8265
     
    9780
    9881    #region Compilation
    99 
    10082    public override Assembly Compile() {
    10183      DeregisterScriptEvents();
     
    10991    #endregion
    11092
    111     public virtual void ExecuteAsync() {
     93    protected override void ExecuteCode() {
    11294      if (compiledScript == null) return;
    113       executionTime = TimeSpan.Zero;
    114       scriptThread = new Thread(() => {
    115         Exception ex = null;
    116         var timer = new System.Timers.Timer(250) { AutoReset = true };
    117         timer.Elapsed += timer_Elapsed;
    118         try {
    119           OnScriptExecutionStarted();
    120           lastUpdateTime = DateTime.UtcNow;
    121           timer.Start();
    122           compiledScript.Execute(VariableStore);
    123         } catch (Exception e) {
    124           ex = e;
    125         } finally {
    126           scriptThread = null;
    127           timer.Elapsed -= timer_Elapsed;
    128           timer.Stop();
    129           ExecutionTime += DateTime.UtcNow - lastUpdateTime;
    130           OnScriptExecutionFinished(ex);
    131         }
    132       });
    133       scriptThread.SetApartmentState(ApartmentState.STA);
    134       scriptThread.Start();
    135     }
    13695
    137     public virtual void Kill() {
    138       if (scriptThread == null) return;
    139       scriptThread.Abort();
    140     }
    141 
    142     private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
    143       var timer = (System.Timers.Timer)sender;
    144       timer.Enabled = false;
    145       DateTime now = DateTime.UtcNow;
    146       ExecutionTime += now - lastUpdateTime;
    147       lastUpdateTime = now;
    148       timer.Enabled = true;
     96      compiledScript.Execute(VariableStore);
    14997    }
    15098
    15199    protected virtual void CompiledScriptOnConsoleOutputChanged(object sender, EventArgs<string> e) {
    152100      OnConsoleOutputChanged(e.Value);
    153     }
    154 
    155     public event EventHandler ScriptExecutionStarted;
    156     protected virtual void OnScriptExecutionStarted() {
    157       var handler = ScriptExecutionStarted;
    158       if (handler != null) handler(this, EventArgs.Empty);
    159     }
    160 
    161     public event EventHandler<EventArgs<Exception>> ScriptExecutionFinished;
    162     protected virtual void OnScriptExecutionFinished(Exception e) {
    163       var handler = ScriptExecutionFinished;
    164       if (handler != null) handler(this, new EventArgs<Exception>(e));
    165101    }
    166102
     
    170106      if (handler != null) handler(this, new EventArgs<string>(args));
    171107    }
    172 
    173     public event EventHandler ExecutionTimeChanged;
    174     protected virtual void OnExecutionTimeChanged() {
    175       var handler = ExecutionTimeChanged;
    176       if (handler != null) handler(this, EventArgs.Empty);
    177     }
    178108  }
    179109}
Note: See TracChangeset for help on using the changeset viewer.