- Timestamp:
- 11/23/15 21:18:26 (9 years ago)
- Location:
- branches/Async/HeuristicLab.Optimization/3.3/MetaOptimizers
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Async/HeuristicLab.Optimization/3.3/MetaOptimizers/BatchRun.cs
r12504 r13349 23 23 using System.Collections.Generic; 24 24 using System.Drawing; 25 using System.Threading; 26 using System.Threading.Tasks; 25 27 using HeuristicLab.Collections; 26 28 using HeuristicLab.Common; … … 39 41 [StorableClass] 40 42 public sealed class BatchRun : NamedItem, IOptimizer, IStorableContent { 43 private readonly ManualResetEvent signaler = new ManualResetEvent(true); 44 41 45 public string Filename { get; set; } 42 46 … … 244 248 } 245 249 public void Start() { 246 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 247 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 248 if (Optimizer == null) return; 249 batchRunAction = BatchRunAction.Start; 250 if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare(); 251 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 252 try { Optimizer.Start(); } 253 catch (InvalidOperationException) { } 250 StartAsync().Wait(); 251 } 252 public async Task StartAsync() { 253 await StartAsync(new CancellationToken()); 254 } 255 public async Task StartAsync(CancellationToken cancellationToken) { 256 signaler.Reset(); 257 await Task.Run(async () => { 258 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 259 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 260 if (Optimizer == null) return; 261 batchRunAction = BatchRunAction.Start; 262 if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare(); 263 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 264 try { await Optimizer.StartAsync(cancellationToken); } 265 catch (InvalidOperationException) { } 266 signaler.WaitOne(); 267 }, cancellationToken); 254 268 } 255 269 public void Pause() { … … 326 340 batchRunAction = BatchRunAction.None; 327 341 ExecutionState = ExecutionState.Paused; 342 signaler.Set(); 328 343 EventHandler handler = Paused; 329 344 if (handler != null) handler(this, EventArgs.Empty); … … 333 348 batchRunAction = BatchRunAction.None; 334 349 ExecutionState = ExecutionState.Stopped; 350 signaler.Set(); 335 351 EventHandler handler = Stopped; 336 352 if (handler != null) handler(this, EventArgs.Empty); … … 397 413 else if (batchRunAction == BatchRunAction.Start) { 398 414 Optimizer.Prepare(); 399 Optimizer.Start ();415 Optimizer.StartAsync(); 400 416 } else if (executionState == ExecutionState.Started) { 401 417 // if the batch run hasn't been started but the inner optimizer was run then pause -
branches/Async/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs
r13000 r13349 25 25 using System.Linq; 26 26 using System.Threading; 27 using System.Threading.Tasks; 27 28 using HeuristicLab.Collections; 28 29 using HeuristicLab.Common; … … 38 39 [StorableClass] 39 40 public sealed class Experiment : NamedItem, IOptimizer, IStorableContent { 41 private readonly ManualResetEvent signaler = new ManualResetEvent(true); 42 40 43 public string Filename { get; set; } 41 44 … … 186 189 } 187 190 public void Start() { 188 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 189 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 190 if (Optimizers.Count == 0) return; 191 192 experimentStarted = true; 193 experimentStopped = false; 194 IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)); 195 if (optimizer != null) { 196 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 197 try { optimizer.Start(); } 198 catch (InvalidOperationException) { } 199 } 191 StartAsync().Wait(); 192 } 193 public async Task StartAsync() { 194 await StartAsync(new CancellationToken()); 195 } 196 public async Task StartAsync(CancellationToken cancellationToken) { 197 signaler.Reset(); 198 await Task.Run(async () => { 199 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 200 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 201 if (Optimizers.Count == 0) return; 202 203 experimentStarted = true; 204 experimentStopped = false; 205 IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)); 206 if (optimizer != null) { 207 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 208 try { await optimizer.StartAsync(cancellationToken); } 209 catch (InvalidOperationException) { } 210 } 211 signaler.WaitOne(); 212 }, cancellationToken); 200 213 } 201 214 public void Pause() { … … 261 274 private void OnPaused() { 262 275 ExecutionState = ExecutionState.Paused; 276 signaler.Set(); 263 277 EventHandler handler = Paused; 264 278 if (handler != null) handler(this, EventArgs.Empty); … … 267 281 private void OnStopped() { 268 282 ExecutionState = ExecutionState.Stopped; 283 signaler.Set(); 269 284 EventHandler handler = Stopped; 270 285 if (handler != null) handler(this, EventArgs.Empty); … … 380 395 } else { 381 396 if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) { 382 Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start ();397 Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).StartAsync(); 383 398 } else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped(); 384 399 else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused(); -
branches/Async/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs
r13321 r13349 25 25 using System.Drawing; 26 26 using System.Linq; 27 using System.Threading; 27 28 using System.Threading.Tasks; 28 29 using HeuristicLab.Collections; … … 40 41 [StorableClass] 41 42 public sealed class TimeLimitRun : NamedItem, IOptimizer, IStorableContent, INotifyPropertyChanged { 43 private readonly ManualResetEvent signaler = new ManualResetEvent(true); 44 42 45 public string Filename { get; set; } 43 46 … … 238 241 } 239 242 public void Start() { 240 Algorithm.Start(); 243 StartAsync().Wait(); 244 } 245 public async Task StartAsync() { 246 await StartAsync(new CancellationToken()); 247 } 248 public async Task StartAsync(CancellationToken cancellationToken) { 249 signaler.Reset(); 250 await Task.Run(async () => { 251 await Algorithm.StartAsync(cancellationToken); 252 signaler.WaitOne(); 253 }, cancellationToken); 241 254 } 242 255 public void Pause() { … … 282 295 public event EventHandler Paused; 283 296 private void OnPaused() { 297 if (!pausedForSnapshot && !pausedForTermination) signaler.Set(); 284 298 var handler = Paused; 285 299 if (handler != null) handler(this, EventArgs.Empty); … … 287 301 public event EventHandler Stopped; 288 302 private void OnStopped() { 303 signaler.Set(); 289 304 var handler = Stopped; 290 305 if (handler != null) handler(this, EventArgs.Empty); … … 336 351 private void Algorithm_Paused(object sender, EventArgs e) { 337 352 var action = pausedForTermination ? ExecutionState.Stopped : (pausedForSnapshot ? ExecutionState.Started : ExecutionState.Paused); 338 if (pausedForSnapshot || pausedForTermination) {339 pausedForSnapshot = pausedForTermination = false;353 bool pausedByLimit = pausedForSnapshot || pausedForTermination; 354 if (pausedByLimit) { 340 355 MakeSnapshot(); 341 356 FindNextSnapshotTimeIndex(ExecutionTime); 342 357 } 343 358 OnPaused(); 344 if (action == ExecutionState.Started) Algorithm.Start(); 359 if (pausedByLimit) pausedForSnapshot = pausedForTermination = false; 360 if (action == ExecutionState.Started) Algorithm.StartAsync(); 345 361 else if (action == ExecutionState.Stopped) Algorithm.Stop(); 346 362 }
Note: See TracChangeset
for help on using the changeset viewer.