Changeset 5193 for trunk/sources/HeuristicLab.Core/3.3
- Timestamp:
- 01/03/11 00:46:55 (14 years ago)
- Location:
- trunk/sources
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources
- Property svn:mergeinfo changed
/branches/ParallelEngine (added) merged: 5175-5178,5183,5185,5187-5188
- Property svn:mergeinfo changed
-
trunk/sources/HeuristicLab.Core/3.3/Engine.cs
r4722 r5193 23 23 using System.Collections.Generic; 24 24 using System.Threading; 25 using System.Threading.Tasks; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 42 43 } 43 44 44 private bool pausePending, stopPending; 45 #region Variables for communication between threads 46 private CancellationTokenSource cancellationTokenSource; 47 private bool stopPending; 45 48 private DateTime lastUpdateTime; 46 private System.Timers.Timer timer;49 #endregion 47 50 48 51 [StorableConstructor] 49 protected Engine(bool deserializing) 50 : base(deserializing) { 51 pausePending = stopPending = false; 52 timer = new System.Timers.Timer(100); 53 timer.AutoReset = true; 54 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); 55 } 52 protected Engine(bool deserializing) : base(deserializing) { } 56 53 protected Engine(Engine original, Cloner cloner) 57 54 : base(original, cloner) { … … 62 59 for (int i = contexts.Length - 1; i >= 0; i--) 63 60 executionStack.Push(cloner.Clone(contexts[i])); 64 pausePending = original.pausePending;65 stopPending = original.stopPending;66 timer = new System.Timers.Timer(100);67 timer.AutoReset = true;68 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);69 61 } 70 62 protected Engine() … … 72 64 log = new Log(); 73 65 executionStack = new Stack<IOperation>(); 74 pausePending = stopPending = false;75 timer = new System.Timers.Timer(100);76 timer.AutoReset = true;77 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);78 66 } 79 67 … … 97 85 public override void Start() { 98 86 base.Start(); 99 ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null); 87 cancellationTokenSource = new CancellationTokenSource(); 88 stopPending = false; 89 Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token); 90 task.ContinueWith(t => { 91 try { 92 t.Wait(); 93 } 94 catch (AggregateException ex) { 95 try { 96 ex.Flatten().Handle(x => x is OperationCanceledException); 97 } 98 catch (AggregateException remaining) { 99 if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]); 100 else OnExceptionOccurred(remaining); 101 } 102 } 103 cancellationTokenSource.Dispose(); 104 cancellationTokenSource = null; 105 if (stopPending) executionStack.Clear(); 106 if (executionStack.Count == 0) OnStopped(); 107 else OnPaused(); 108 }); 100 109 } 101 110 protected override void OnStarted() { … … 106 115 public override void Pause() { 107 116 base.Pause(); 108 pausePending = true;117 cancellationTokenSource.Cancel(); 109 118 } 110 119 protected override void OnPaused() { … … 115 124 public override void Stop() { 116 125 base.Stop(); 117 stopPending = true; 118 if (ExecutionState == ExecutionState.Paused) OnStopped(); 126 if (ExecutionState == ExecutionState.Paused) { 127 executionStack.Clear(); 128 OnStopped(); 129 } else { 130 stopPending = true; 131 cancellationTokenSource.Cancel(); 132 } 119 133 } 120 134 protected override void OnStopped() { … … 129 143 130 144 private void Run(object state) { 145 CancellationToken cancellationToken = (CancellationToken)state; 146 131 147 OnStarted(); 132 pausePending = stopPending = false; 148 lastUpdateTime = DateTime.Now; 149 System.Timers.Timer timer = new System.Timers.Timer(100); 150 timer.AutoReset = true; 151 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); 152 timer.Start(); 153 try { 154 Run(cancellationToken); 155 } 156 finally { 157 timer.Stop(); 158 timer.Dispose(); 159 ExecutionTime += DateTime.Now - lastUpdateTime; 160 } 133 161 134 lastUpdateTime = DateTime.Now; 135 timer.Start(); 136 while (!pausePending && !stopPending && (executionStack.Count > 0)) { 137 ProcessNextOperation(); 138 } 139 timer.Stop(); 140 ExecutionTime += DateTime.Now - lastUpdateTime; 141 142 if (pausePending) OnPaused(); 143 else OnStopped(); 162 cancellationToken.ThrowIfCancellationRequested(); 144 163 } 145 146 protected abstract void ProcessNextOperation(); 164 protected abstract void Run(CancellationToken cancellationToken); 147 165 148 166 private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { -
trunk/sources/HeuristicLab.Core/3.3/Interfaces/IOperator.cs
r2845 r5193 21 21 22 22 using System; 23 using System.Threading; 23 24 24 25 namespace HeuristicLab.Core { 25 26 /// <summary> 26 /// Interface to represent an operator (e.g. GreaterThanComparator,...), 27 /// a basic instruction of an algorithm. 27 /// Interface to represent an operator. 28 28 /// </summary> 29 29 public interface IOperator : IParameterizedNamedItem { 30 /// <summary>31 /// Gets or sets a boolean value whether the engine should stop here during the run.32 /// </summary>33 30 bool Breakpoint { get; set; } 34 31 35 /// <summary> 36 /// Executes the current instance on the specified <paramref name="scope"/>. 37 /// </summary> 38 /// <param name="scope">The scope where to execute the current instance.</param> 39 /// <returns>The next operation.</returns> 40 IOperation Execute(IExecutionContext context); 41 /// <summary> 42 /// Aborts the current operator. 43 /// </summary> 44 void Abort(); 32 IOperation Execute(IExecutionContext context, CancellationToken cancellationToken); 45 33 46 /// <summary> 47 /// Occurs when the breakpoint flag of the current instance was changed. 48 /// </summary> 49 event EventHandler BreakpointChanged; 50 /// <summary> 51 /// Occurs when the current instance is executed. 52 /// </summary> 34 event EventHandler BreakpointChanged; 53 35 event EventHandler Executed; 54 36 }
Note: See TracChangeset
for help on using the changeset viewer.