Changeset 5185 for branches/ParallelEngine/HeuristicLab.Core
- Timestamp:
- 12/31/10 04:29:19 (14 years ago)
- Location:
- branches/ParallelEngine/HeuristicLab.Core/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ParallelEngine/HeuristicLab.Core/3.3/Engine.cs
r4722 r5185 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 stopRequested; 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 stopRequested = 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 ex.Flatten().Handle(x => { 96 if (!(x is OperationCanceledException)) OnExceptionOccurred(x); 97 return true; 98 }); 99 } 100 cancellationTokenSource.Dispose(); 101 cancellationTokenSource = null; 102 if (stopRequested) { 103 executionStack.Clear(); 104 OnStopped(); 105 } else { 106 if (executionStack.Count == 0) OnStopped(); 107 else OnPaused(); 108 } 109 }); 100 110 } 101 111 protected override void OnStarted() { … … 106 116 public override void Pause() { 107 117 base.Pause(); 108 pausePending = true;118 cancellationTokenSource.Cancel(); 109 119 } 110 120 protected override void OnPaused() { … … 115 125 public override void Stop() { 116 126 base.Stop(); 117 stopPending = true;118 127 if (ExecutionState == ExecutionState.Paused) OnStopped(); 128 else { 129 stopRequested = true; 130 cancellationTokenSource.Cancel(); 131 } 119 132 } 120 133 protected override void OnStopped() { … … 129 142 130 143 private void Run(object state) { 144 CancellationToken cancellationToken = (CancellationToken)state; 145 131 146 OnStarted(); 132 pausePending = stopPending = false; 147 lastUpdateTime = DateTime.Now; 148 System.Timers.Timer timer = new System.Timers.Timer(100); 149 timer.AutoReset = true; 150 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); 151 timer.Start(); 152 try { 153 Run(cancellationToken); 154 } 155 finally { 156 timer.Stop(); 157 timer.Dispose(); 158 ExecutionTime += DateTime.Now - lastUpdateTime; 159 } 133 160 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(); 161 cancellationToken.ThrowIfCancellationRequested(); 144 162 } 145 146 protected abstract void ProcessNextOperation(); 163 protected abstract void Run(CancellationToken cancellationToken); 147 164 148 165 private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { -
branches/ParallelEngine/HeuristicLab.Core/3.3/Interfaces/IOperator.cs
r5183 r5185 21 21 22 22 using System; 23 using System.Threading; 23 24 24 25 namespace HeuristicLab.Core { … … 29 30 bool Breakpoint { get; set; } 30 31 31 IOperation Execute(IExecutionContext context); 32 void Abort(); 32 IOperation Execute(IExecutionContext context, CancellationToken cancellationToken); 33 33 34 34 event EventHandler BreakpointChanged;
Note: See TracChangeset
for help on using the changeset viewer.