using System; using System.Threading; using HeuristicLab.Optimization; namespace HeuristicLab.Problems.MetaOptimization { public static class AlgorithmExtensions { public static void StartSync(this IAlgorithm algorithm) { var executor = new AlgorithmExecutor(algorithm); executor.StartSync(); } } /// /// Can execute an algorithm synchronously /// internal class AlgorithmExecutor { private IAlgorithm algorithm; private AutoResetEvent waitHandle = new AutoResetEvent(false); public AlgorithmExecutor(IAlgorithm algorithm) { this.algorithm = algorithm; } public void StartSync() { algorithm.Stopped += new EventHandler(algorithm_Stopped); algorithm.Paused += new EventHandler(algorithm_Paused); algorithm.Start(); waitHandle.WaitOne(); waitHandle.Dispose(); algorithm.Stopped -= new EventHandler(algorithm_Stopped); algorithm.Paused -= new EventHandler(algorithm_Paused); } void algorithm_Paused(object sender, EventArgs e) { waitHandle.Set(); } void algorithm_Stopped(object sender, EventArgs e) { waitHandle.Set(); } } }