Changeset 6024 for branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/AlgorithmExecutor.cs
 Timestamp:
 04/18/11 23:27:42 (13 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/AlgorithmExecutor.cs
r6018 r6024 6 6 7 7 public static class AlgorithmExtensions { 8 public static void StartSync(this IAlgorithm algorithm) {9 var executor = new AlgorithmExecutor(algorithm);10 executor.StartSync();11 }12 8 public static void StartSync(this IAlgorithm algorithm, CancellationToken cancellationToken) { 13 9 var executor = new AlgorithmExecutor(algorithm, cancellationToken); … … 23 19 private AutoResetEvent waitHandle = new AutoResetEvent(false); 24 20 private CancellationToken cancellationToken; 25 private bool useCancellationToken = false;26 27 public AlgorithmExecutor(IAlgorithm algorithm) {28 this.algorithm = algorithm;29 }30 21 31 22 public AlgorithmExecutor(IAlgorithm algorithm, CancellationToken cancellationToken) { 32 23 this.algorithm = algorithm; 33 24 this.cancellationToken = cancellationToken; 34 this.useCancellationToken = true;35 25 } 36 26 … … 38 28 algorithm.Stopped += new EventHandler(algorithm_Stopped); 39 29 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))) { 44 32 algorithm.Start(); 33 waitHandle.WaitOne(); 34 waitHandle.Dispose(); 45 35 } 46 36 47 waitHandle.WaitOne();48 waitHandle.Dispose();49 37 algorithm.Stopped = new EventHandler(algorithm_Stopped); 50 38 algorithm.Paused = new EventHandler(algorithm_Paused); 39 if (algorithm.ExecutionState == Core.ExecutionState.Started) { 40 algorithm.Pause(); 41 } 42 cancellationToken.ThrowIfCancellationRequested(); 51 43 } 52 44 53 void algorithm_Paused(object sender, EventArgs e) {45 private void algorithm_Paused(object sender, EventArgs e) { 54 46 waitHandle.Set(); 55 47 } 56 48 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() { 58 54 waitHandle.Set(); 59 55 }
Note: See TracChangeset
for help on using the changeset viewer.