Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/18/11 23:27:42 (13 years ago)
Author:
cneumuel
Message:

#1215

  • improved the way AlgorithmExecutor handles cancellation
Location:
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/AlgorithmExecutor.cs

    r6018 r6024  
    66
    77  public static class AlgorithmExtensions {
    8     public static void StartSync(this IAlgorithm algorithm) {
    9       var executor = new AlgorithmExecutor(algorithm);
    10       executor.StartSync();
    11     }
    128    public static void StartSync(this IAlgorithm algorithm, CancellationToken cancellationToken) {
    139      var executor = new AlgorithmExecutor(algorithm, cancellationToken);
     
    2319    private AutoResetEvent waitHandle = new AutoResetEvent(false);
    2420    private CancellationToken cancellationToken;
    25     private bool useCancellationToken = false;
    26 
    27     public AlgorithmExecutor(IAlgorithm algorithm) {
    28       this.algorithm = algorithm;
    29     }
    3021
    3122    public AlgorithmExecutor(IAlgorithm algorithm, CancellationToken cancellationToken) {
    3223      this.algorithm = algorithm;
    3324      this.cancellationToken = cancellationToken;
    34       this.useCancellationToken = true;
    3525    }
    3626
     
    3828      algorithm.Stopped += new EventHandler(algorithm_Stopped);
    3929      algorithm.Paused += new EventHandler(algorithm_Paused);
    40      
    41       if(useCancellationToken && algorithm is EngineAlgorithm && ((EngineAlgorithm)algorithm).Engine is SequentialEngine.SequentialEngine) {
    42         ((SequentialEngine.SequentialEngine)((EngineAlgorithm)algorithm).Engine).Start(cancellationToken);
    43       } else {
     30
     31      using (CancellationTokenRegistration registration = cancellationToken.Register(new Action(cancellationToken_Canceled))) {
    4432        algorithm.Start();
     33        waitHandle.WaitOne();
     34        waitHandle.Dispose();
    4535      }
    4636
    47       waitHandle.WaitOne();
    48       waitHandle.Dispose();
    4937      algorithm.Stopped -= new EventHandler(algorithm_Stopped);
    5038      algorithm.Paused -= new EventHandler(algorithm_Paused);
     39      if (algorithm.ExecutionState == Core.ExecutionState.Started) {
     40        algorithm.Pause();
     41      }
     42      cancellationToken.ThrowIfCancellationRequested();
    5143    }
    5244
    53     void algorithm_Paused(object sender, EventArgs e) {
     45    private void algorithm_Paused(object sender, EventArgs e) {
    5446      waitHandle.Set();
    5547    }
    5648
    57     void algorithm_Stopped(object sender, EventArgs e) {
     49    private void algorithm_Stopped(object sender, EventArgs e) {
     50      waitHandle.Set();
     51    }
     52
     53    private void cancellationToken_Canceled() {
    5854      waitHandle.Set();
    5955    }
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/AlgorithmEvaluator.cs

    r6018 r6024  
    6464      var sw = new Stopwatch();
    6565      sw.Start();
     66      //// prepare and clear is needed, if the algorithm has been started and stopped before (this happens when meta level algorithm is paused)
     67      //algorithm.Prepare(); // <--- SHOULD NOT HAPPEN!
     68      //algorithm.Runs.Clear();
     69
    6670      algorithm.StartSync(CancellationToken);
    6771      sw.Stop();
Note: See TracChangeset for help on using the changeset viewer.