Changeset 15339


Ignore:
Timestamp:
08/22/17 15:15:38 (2 years ago)
Author:
pfleck
Message:

#2823 Moved wait-handle .Set to OnPaused/OnStopped.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs

    r15327 r15339  
    111111    private bool experimentStarted = false;
    112112    private bool experimentStopped = false;
    113     private ManualResetEventSlim allOptimizerFinished; // this indicates that all started optimizers have been paused or stopped
     113    private readonly ManualResetEventSlim allOptimizerFinished = new ManualResetEventSlim(false); // this indicates that all started optimizers have been paused or stopped
    114114
    115115    public Experiment()
     
    192192      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
    193193        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
    194       if (Optimizers.Count == 0) return;
     194      if (!Optimizers.Any(x => x.ExecutionState == ExecutionState.Prepared || x.ExecutionState == ExecutionState.Paused)) return;
    195195
    196196      experimentStarted = true;
    197197      experimentStopped = false;
    198       allOptimizerFinished = new ManualResetEventSlim(false);
    199 
     198      allOptimizerFinished.Reset();
    200199      IOptimizer optimizer;
    201       while ((optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) != null) {
     200      while ((optimizer = Optimizers.FirstOrDefault(x => x.ExecutionState == ExecutionState.Prepared || x.ExecutionState == ExecutionState.Paused)) != null) {
    202201        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    203202        try { optimizer.Start(cancellationToken); } catch (InvalidOperationException) { }
    204203        if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break;
    205204      }
    206 
    207205      allOptimizerFinished.Wait();
    208206    }
     
    271269    private void OnPaused() {
    272270      ExecutionState = ExecutionState.Paused;
     271      allOptimizerFinished.Set();
    273272      EventHandler handler = Paused;
    274273      if (handler != null) handler(this, EventArgs.Empty);
     
    277276    private void OnStopped() {
    278277      ExecutionState = ExecutionState.Stopped;
     278      allOptimizerFinished.Set();
    279279      EventHandler handler = Stopped;
    280280      if (handler != null) handler(this, EventArgs.Empty);
     
    373373    private void optimizer_Paused(object sender, EventArgs e) {
    374374      lock (locker)
    375         if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) {
    376           OnPaused();
    377           allOptimizerFinished.Set();
    378         }
     375        if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) OnPaused();
    379376    }
    380377    private void optimizer_Prepared(object sender, EventArgs e) {
     
    389386      lock (locker) {
    390387        if (experimentStopped) {
    391           if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) {
    392             OnStopped();
    393             allOptimizerFinished.Set();
    394           }
     388          if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) OnStopped();
    395389        } else {
    396390          if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) return;
    397           else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) {
    398             OnStopped();
    399             allOptimizerFinished.Set();
    400           } else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) {
    401             OnPaused();
    402             allOptimizerFinished.Set();
    403           }
     391          else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped();
     392          else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused();
    404393        }
    405394      }
Note: See TracChangeset for help on using the changeset viewer.