[6421] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[5653] | 23 | using System.Threading;
|
---|
[6445] | 24 | using HeuristicLab.Core;
|
---|
[5653] | 25 |
|
---|
| 26 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
| 27 | public static class AlgorithmExtensions {
|
---|
[6445] | 28 | public static void StartSync(this IExecutable executable, CancellationToken cancellationToken) {
|
---|
| 29 | var executor = new AlgorithmExecutor(executable, cancellationToken);
|
---|
[6018] | 30 | executor.StartSync();
|
---|
| 31 | }
|
---|
[5653] | 32 | }
|
---|
| 33 |
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// Can execute an algorithm synchronously
|
---|
| 36 | /// </summary>
|
---|
| 37 | internal class AlgorithmExecutor {
|
---|
[6445] | 38 | private IExecutable executable;
|
---|
[5653] | 39 | private AutoResetEvent waitHandle = new AutoResetEvent(false);
|
---|
[6018] | 40 | private CancellationToken cancellationToken;
|
---|
[5653] | 41 |
|
---|
[6445] | 42 | public AlgorithmExecutor(IExecutable executable, CancellationToken cancellationToken) {
|
---|
| 43 | this.executable = executable;
|
---|
[6018] | 44 | this.cancellationToken = cancellationToken;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[5653] | 47 | public void StartSync() {
|
---|
[6445] | 48 | executable.Stopped += new EventHandler(algorithm_Stopped);
|
---|
| 49 | executable.Paused += new EventHandler(algorithm_Paused);
|
---|
[6024] | 50 |
|
---|
| 51 | using (CancellationTokenRegistration registration = cancellationToken.Register(new Action(cancellationToken_Canceled))) {
|
---|
[6445] | 52 | executable.Start();
|
---|
[6024] | 53 | waitHandle.WaitOne();
|
---|
| 54 | waitHandle.Dispose();
|
---|
[6018] | 55 | }
|
---|
| 56 |
|
---|
[6445] | 57 | executable.Stopped -= new EventHandler(algorithm_Stopped);
|
---|
| 58 | executable.Paused -= new EventHandler(algorithm_Paused);
|
---|
| 59 | if (executable.ExecutionState == Core.ExecutionState.Started) {
|
---|
| 60 | executable.Pause();
|
---|
[6024] | 61 | }
|
---|
| 62 | cancellationToken.ThrowIfCancellationRequested();
|
---|
[5653] | 63 | }
|
---|
| 64 |
|
---|
[6024] | 65 | private void algorithm_Paused(object sender, EventArgs e) {
|
---|
[5653] | 66 | waitHandle.Set();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[6024] | 69 | private void algorithm_Stopped(object sender, EventArgs e) {
|
---|
[5653] | 70 | waitHandle.Set();
|
---|
| 71 | }
|
---|
[6024] | 72 |
|
---|
| 73 | private void cancellationToken_Canceled() {
|
---|
| 74 | waitHandle.Set();
|
---|
| 75 | }
|
---|
[5653] | 76 | }
|
---|
| 77 | }
|
---|