Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/AlgorithmExecutor.cs @ 12803

Last change on this file since 12803 was 6445, checked in by cneumuel, 14 years ago

#1215

  • minor changes
File size: 2.6 KB
Line 
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
22using System;
23using System.Threading;
24using HeuristicLab.Core;
25
26namespace HeuristicLab.Problems.MetaOptimization {
27  public static class AlgorithmExtensions {
28    public static void StartSync(this IExecutable executable, CancellationToken cancellationToken) {
29      var executor = new AlgorithmExecutor(executable, cancellationToken);
30      executor.StartSync();
31    }
32  }
33
34  /// <summary>
35  /// Can execute an algorithm synchronously
36  /// </summary>
37  internal class AlgorithmExecutor {
38    private IExecutable executable;
39    private AutoResetEvent waitHandle = new AutoResetEvent(false);
40    private CancellationToken cancellationToken;
41
42    public AlgorithmExecutor(IExecutable executable, CancellationToken cancellationToken) {
43      this.executable = executable;
44      this.cancellationToken = cancellationToken;
45    }
46
47    public void StartSync() {
48      executable.Stopped += new EventHandler(algorithm_Stopped);
49      executable.Paused += new EventHandler(algorithm_Paused);
50
51      using (CancellationTokenRegistration registration = cancellationToken.Register(new Action(cancellationToken_Canceled))) {
52        executable.Start();
53        waitHandle.WaitOne();
54        waitHandle.Dispose();
55      }
56
57      executable.Stopped -= new EventHandler(algorithm_Stopped);
58      executable.Paused -= new EventHandler(algorithm_Paused);
59      if (executable.ExecutionState == Core.ExecutionState.Started) {
60        executable.Pause();
61      }
62      cancellationToken.ThrowIfCancellationRequested();
63    }
64
65    private void algorithm_Paused(object sender, EventArgs e) {
66      waitHandle.Set();
67    }
68
69    private void algorithm_Stopped(object sender, EventArgs e) {
70      waitHandle.Set();
71    }
72
73    private void cancellationToken_Canceled() {
74      waitHandle.Set();
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.