Changeset 11481 for branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/AlgorithmServiceNode.cs
- Timestamp:
- 10/18/14 03:25:58 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/AlgorithmServiceNode.cs
r11468 r11481 25 25 using System; 26 26 using System.Collections.Generic; 27 using System.Linq; 27 28 using System.Threading; 28 using System.Linq;29 29 30 30 namespace HeuristicLab.Optimization.Networks { … … 34 34 private object locker = new object(); 35 35 private Dictionary<IAlgorithm, AutoResetEvent> waitHandles = new Dictionary<IAlgorithm,AutoResetEvent>(); 36 private Dictionary<IAlgorithm, Exception> exceptions = new Dictionary<IAlgorithm, Exception>(); 36 37 37 38 new public PortCollection Ports { … … 118 119 algorithm.Runs.Clear(); 119 120 algorithm.Prepare(); 121 algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred; 120 122 algorithm.Stopped += Algorithm_Stopped; 121 algorithm.Start(); 122 if (WaitHandle.WaitAny(new WaitHandle[] { waitHandles[algorithm], token.WaitHandle }) == 1) { 123 // retrieve results 124 foreach (var p in parameters.Where(x => x.Type == ServiceParameterType.Output)) { 125 IResult result = null; 126 if (algorithm.Results.TryGetValue(p.Name, out result)) { 127 p.Value = result.Value; 128 } 129 } 130 131 lock (locker) { 132 waitHandles[algorithm].Dispose(); 133 waitHandles.Remove(algorithm); 134 Runs.Add(algorithm.Runs.ToArray()[0]); 135 } 136 } else { // cancellation 137 algorithm.Stop(); 138 lock (locker) { 139 waitHandles[algorithm].Dispose(); 140 waitHandles.Remove(algorithm); 141 } 142 } 143 } 144 123 124 using (token.Register(() => { algorithm.Stop(); })) { 125 algorithm.Start(); 126 waitHandles[algorithm].WaitOne(); 127 } 128 129 lock (locker) { 130 waitHandles[algorithm].Dispose(); 131 waitHandles.Remove(algorithm); 132 133 Exception ex = null; 134 if (exceptions.TryGetValue(algorithm, out ex)) { 135 exceptions.Remove(algorithm); 136 throw ex; 137 } 138 } 139 140 // retrieve results 141 var run = algorithm.Runs.First(); 142 foreach (var p in parameters.Where(x => x.Type == ServiceParameterType.Output)) { 143 IItem result = null; 144 if (run.Results.TryGetValue(p.Name, out result)) { 145 p.Value = result; 146 } 147 } 148 149 lock (locker) { 150 Runs.Add(run); 151 } 152 } 153 154 private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) { 155 var algorithm = (IAlgorithm)sender; 156 lock (locker) { 157 exceptions.Add(algorithm, e.Value); 158 } 159 algorithm.Stop(); 160 } 145 161 private void Algorithm_Stopped(object sender, EventArgs e) { 146 162 lock (locker) {
Note: See TracChangeset
for help on using the changeset viewer.