Changeset 11682 for branches/OptimizationNetworks
- Timestamp:
- 12/14/14 01:02:09 (10 years ago)
- Location:
- branches/OptimizationNetworks
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Core/3.3/Networks/MessagePort.cs
r11577 r11682 182 182 LastMessage = message; 183 183 OnMessageReceived(message, token); 184 token.ThrowIfCancellationRequested(); 184 185 } 185 186 public event EventHandler<EventArgs<IMessage, CancellationToken>> MessageReceived; -
branches/OptimizationNetworks/HeuristicLab.Networks/3.3/AlgorithmNode.cs
r11577 r11682 36 36 public class AlgorithmNode : Node, IAlgorithmNode { 37 37 private object locker = new object(); 38 private Dictionary<IAlgorithm, AutoResetEvent> waitHandles = new Dictionary<IAlgorithm, AutoResetEvent>();39 private Dictionary<IAlgorithm, Exception> exceptions = new Dictionary<IAlgorithm, Exception>();40 38 41 39 new public PortCollection Ports { … … 115 113 } 116 114 algorithm = (IAlgorithm)Algorithm.Clone(cloner); 117 waitHandles.Add(algorithm, new AutoResetEvent(false));118 115 } 119 116 … … 131 128 } 132 129 133 algorithm.StoreAlgorithmInEachRun = false; 134 algorithm.Prepare(true); 135 algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred; 136 algorithm.Stopped += Algorithm_Stopped; 137 138 using (token.Register(() => { algorithm.Stop(); })) { 130 token.ThrowIfCancellationRequested(); 131 132 Exception exception = null; 133 using (var started = new AutoResetEvent(false)) 134 using (var stopped = new AutoResetEvent(false)) 135 using (var ctr = token.Register(() => { 136 started.WaitOne(); 137 try { algorithm.Stop(); } 138 catch (InvalidOperationException) { 139 // NOTE: 140 // After the algorithm's engine is stopped, all stateful items in the algorithm are cleared before the execution state of the algorithm is set to stopped. 141 // When algorithm.Stop() is called during that operation, an InvalidOperationException is thrown because Stop is called on the engine but the engine is already stopped. 142 // This exception can be ignored, as the algorithm will stop immediately after the clearing of its stateful items is finished. 143 } 144 })) { 145 algorithm.StoreAlgorithmInEachRun = false; 146 algorithm.Prepare(true); 147 algorithm.ExceptionOccurred += (sender, args) => { exception = args.Value; }; 148 algorithm.Started += (sender, args) => { started.Set(); }; 149 algorithm.Stopped += (sender, args) => { stopped.Set(); }; 139 150 algorithm.Start(); 140 waitHandles[algorithm].WaitOne(); 141 } 142 143 lock (locker) { 144 waitHandles[algorithm].Dispose(); 145 waitHandles.Remove(algorithm); 146 147 Exception ex = null; 148 if (exceptions.TryGetValue(algorithm, out ex)) { 149 exceptions.Remove(algorithm); 150 throw ex; 151 } 152 } 151 stopped.WaitOne(); 152 } 153 154 if (exception != null) throw exception; 155 token.ThrowIfCancellationRequested(); 153 156 154 157 // retrieve results … … 166 169 } 167 170 168 private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {169 var algorithm = (IAlgorithm)sender;170 lock (locker) {171 exceptions.Add(algorithm, e.Value);172 }173 algorithm.Stop();174 }175 private void Algorithm_Stopped(object sender, EventArgs e) {176 lock (locker) {177 waitHandles[(IAlgorithm)sender].Set();178 }179 }180 181 171 public event EventHandler AlgorithmChanged; 182 172 protected virtual void OnAlgorithmChanged() {
Note: See TracChangeset
for help on using the changeset viewer.