Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/26/17 15:48:32 (5 years ago)
Author:
jkarder
Message:

#2258: merged Async branch into trunk

Location:
trunk/sources
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources

  • trunk/sources/HeuristicLab.Optimization

  • trunk/sources/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs

    r14185 r15287  
    2525using System.Linq;
    2626using System.Threading;
     27using System.Threading.Tasks;
    2728using HeuristicLab.Collections;
    2829using HeuristicLab.Common;
     
    181182      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) {
    182183        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    183         try { optimizer.Prepare(clearRuns); }
    184         catch (InvalidOperationException) { }
     184        try { optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { }
    185185      }
    186186    }
    187187    public void Start() {
     188      Start(CancellationToken.None);
     189    }
     190    public void Start(CancellationToken cancellationToken) {
    188191      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
    189192        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
     
    192195      experimentStarted = true;
    193196      experimentStopped = false;
    194       IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
    195       if (optimizer != null) {
     197      IOptimizer optimizer;
     198      while ((optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) != null) {
    196199        // 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       }
     200        try { optimizer.Start(cancellationToken); } catch (InvalidOperationException) { }
     201        if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break;
     202      }
     203    }
     204    public async Task StartAsync() { await StartAsync(CancellationToken.None); }
     205    public async Task StartAsync(CancellationToken cancellationToken) {
     206      await AsyncHelper.DoAsync(Start, cancellationToken);
    200207    }
    201208    public void Pause() {
     
    208215      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) {
    209216        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    210         try { optimizer.Pause(); }
    211         catch (InvalidOperationException) { }
     217        try { optimizer.Pause(); } catch (InvalidOperationException) { }
    212218      }
    213219    }
     
    222228        foreach (var optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) {
    223229          // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    224           try { optimizer.Stop(); }
    225           catch (InvalidOperationException) { }
     230          try { optimizer.Stop(); } catch (InvalidOperationException) { }
    226231        }
    227232      } else {
     
    357362      try {
    358363        ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime);
    359       }
    360       finally {
     364      } finally {
    361365        Monitor.Exit(locker);
    362366      }
     
    379383          if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) OnStopped();
    380384        } else {
    381           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();
    383           } else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped();
     385          if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) return;
     386          else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped();
    384387          else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused();
    385388        }
Note: See TracChangeset for help on using the changeset viewer.