Changeset 13024


Ignore:
Timestamp:
10/16/15 13:31:14 (3 years ago)
Author:
jkarder
Message:

#2298: added execution time to CSharpScript

Location:
trunk/sources
Files:
3 edited

Legend:

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

    r12012 r13024  
    5252      this.variableStoreView = new HeuristicLab.Scripting.Views.VariableStoreView();
    5353      this.viewHost = new HeuristicLab.MainForm.WindowsForms.ViewHost();
     54      this.executionTimeTextBox = new System.Windows.Forms.TextBox();
     55      this.executionTimeLabel = new System.Windows.Forms.Label();
     56      ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
     57      this.splitContainer1.Panel1.SuspendLayout();
     58      this.splitContainer1.SuspendLayout();
    5459      ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit();
    5560      ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
     
    5863      this.splitContainer2.SuspendLayout();
    5964      this.SuspendLayout();
     65      //
     66      // splitContainer1.Panel1
     67      //
     68      this.splitContainer1.Panel1.Controls.Add(this.executionTimeTextBox);
     69      this.splitContainer1.Panel1.Controls.Add(this.executionTimeLabel);
     70      this.splitContainer1.Panel1.Controls.SetChildIndex(this.executionTimeTextBox, 0);
     71      this.splitContainer1.Panel1.Controls.SetChildIndex(this.executionTimeLabel, 0);
     72      this.splitContainer1.Size = new System.Drawing.Size(637, 543);
    6073      //
    6174      // nameTextBox
     
    132145      this.viewHost.ViewType = null;
    133146      //
     147      // executionTimeTextBox
     148      //
     149      this.executionTimeTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
     150      this.executionTimeTextBox.Location = new System.Drawing.Point(483, 390);
     151      this.executionTimeTextBox.Name = "executionTimeTextBox";
     152      this.executionTimeTextBox.ReadOnly = true;
     153      this.executionTimeTextBox.Size = new System.Drawing.Size(137, 20);
     154      this.executionTimeTextBox.TabIndex = 17;
     155      //
     156      // executionTimeLabel
     157      //
     158      this.executionTimeLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
     159      this.executionTimeLabel.AutoSize = true;
     160      this.executionTimeLabel.Location = new System.Drawing.Point(394, 393);
     161      this.executionTimeLabel.Name = "executionTimeLabel";
     162      this.executionTimeLabel.Size = new System.Drawing.Size(83, 13);
     163      this.executionTimeLabel.TabIndex = 16;
     164      this.executionTimeLabel.Text = "&Execution Time:";
     165      //
    134166      // ScriptView
    135167      //
     
    144176      this.Controls.SetChildIndex(this.nameTextBox, 0);
    145177      this.Controls.SetChildIndex(this.infoLabel, 0);
     178      this.splitContainer1.Panel1.ResumeLayout(false);
     179      this.splitContainer1.Panel1.PerformLayout();
     180      ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
     181      this.splitContainer1.ResumeLayout(false);
    146182      this.splitContainer2.Panel1.ResumeLayout(false);
    147183      this.splitContainer2.Panel2.ResumeLayout(false);
     
    159195    protected MainForm.WindowsForms.ViewHost viewHost;
    160196    protected VariableStoreView variableStoreView;
     197    protected TextBox executionTimeTextBox;
     198    protected Label executionTimeLabel;
    161199  }
    162200}
  • trunk/sources/HeuristicLab.Scripting.Views/3.3/CSharpScriptView.cs

    r12012 r13024  
    5454      Content.ScriptExecutionFinished += ContentOnScriptExecutionFinished;
    5555      Content.ConsoleOutputChanged += ContentOnConsoleOutputChanged;
     56      Content.ExecutionTimeChanged += ContentOnExecutionTimeChanged;
    5657    }
    5758
     
    6061      Content.ScriptExecutionFinished -= ContentOnScriptExecutionFinished;
    6162      Content.ConsoleOutputChanged -= ContentOnConsoleOutputChanged;
     63      Content.ExecutionTimeChanged -= ContentOnExecutionTimeChanged;
    6264      base.DeregisterContentEvents();
    6365    }
     
    106108      }
    107109    }
     110    protected virtual void ContentOnExecutionTimeChanged(object sender, EventArgs eventArgs) {
     111      if (InvokeRequired)
     112        Invoke((Action<object, EventArgs>)ContentOnExecutionTimeChanged, sender, eventArgs);
     113      else {
     114        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
     115      }
     116    }
    108117    #endregion
    109118
     
    112121      if (Content == null) {
    113122        variableStoreView.Content = null;
     123        executionTimeTextBox.Text = "-";
    114124      } else {
    115125        variableStoreView.Content = Content.VariableStore;
     126        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
    116127      }
    117128    }
     
    127138      } else
    128139        if (Compile()) {
    129           outputTextBox.Clear();
    130           Running = true;
    131           Content.ExecuteAsync();
    132         }
     140        outputTextBox.Clear();
     141        Running = true;
     142        Content.ExecuteAsync();
     143      }
    133144    }
    134145
  • trunk/sources/HeuristicLab.Scripting/3.3/Scripts/CSharp/CSharpScript.cs

    r12504 r13024  
    4040    #region Fields & Properties
    4141    private CSharpScriptBase compiledScript;
     42    private Thread scriptThread;
     43    private DateTime lastUpdateTime;
    4244
    4345    public string Filename { get; set; }
     
    4749    public VariableStore VariableStore {
    4850      get { return variableStore; }
     51    }
     52
     53    [Storable]
     54    private TimeSpan executionTime;
     55    public TimeSpan ExecutionTime {
     56      get { return executionTime; }
     57      protected set {
     58        executionTime = value;
     59        OnExecutionTimeChanged();
     60      }
    4961    }
    5062    #endregion
     
    5668      : base(original, cloner) {
    5769      variableStore = cloner.Clone(original.variableStore);
     70      executionTime = original.executionTime;
    5871    }
    5972    public CSharpScript() {
    6073      variableStore = new VariableStore();
     74      executionTime = TimeSpan.Zero;
    6175      Code = CodeTemplate;
    6276    }
     
    6478      : base(code) {
    6579      variableStore = new VariableStore();
     80      executionTime = TimeSpan.Zero;
    6681    }
    6782
     
    94109    #endregion
    95110
    96     private Thread scriptThread;
    97111    public virtual void ExecuteAsync() {
    98112      if (compiledScript == null) return;
     113      executionTime = TimeSpan.Zero;
    99114      scriptThread = new Thread(() => {
    100115        Exception ex = null;
     116        var timer = new System.Timers.Timer(250) { AutoReset = true };
     117        timer.Elapsed += timer_Elapsed;
    101118        try {
    102119          OnScriptExecutionStarted();
     120          lastUpdateTime = DateTime.UtcNow;
     121          timer.Start();
    103122          compiledScript.Execute(VariableStore);
    104         }
    105         catch (Exception e) {
     123        } catch (Exception e) {
    106124          ex = e;
    107         }
    108         finally {
     125        } finally {
    109126          scriptThread = null;
     127          timer.Elapsed -= timer_Elapsed;
     128          timer.Stop();
     129          ExecutionTime += DateTime.UtcNow - lastUpdateTime;
    110130          OnScriptExecutionFinished(ex);
    111131        }
     
    118138      if (scriptThread == null) return;
    119139      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;
    120149    }
    121150
     
    141170      if (handler != null) handler(this, new EventArgs<string>(args));
    142171    }
     172
     173    public event EventHandler ExecutionTimeChanged;
     174    protected virtual void OnExecutionTimeChanged() {
     175      var handler = ExecutionTimeChanged;
     176      if (handler != null) handler(this, EventArgs.Empty);
     177    }
    143178  }
    144179}
Note: See TracChangeset for help on using the changeset viewer.