- Timestamp:
- 11/23/15 21:18:26 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.