Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab/3.3/Tests/AlgorithmExtensions.cs @ 6442

Last change on this file since 6442 was 6205, checked in by cneumuel, 14 years ago

#1522

  • simplified check for EventHandler (by simply checking for delegate)
  • special handling types which use a hashtable internally (dictionaries, hashset, hashtable, ...) (as discussed with swagner)
  • added testcase which shows a stack overflow if ThreadLocal is included and compiler is on Debug (it does not happen in Release)
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;
25using HeuristicLab.Optimization;
26
27namespace HeuristicLab_33.Tests {
28  public static class AlgorithmExtensions {
29    public static void StartSync(this IAlgorithm algorithm, CancellationToken cancellationToken) {
30      var executor = new AlgorithmExecutor(algorithm, cancellationToken);
31      executor.StartSync();
32    }
33  }
34
35  /// <summary>
36  /// Can execute an algorithm synchronously
37  /// </summary>
38  internal class AlgorithmExecutor {
39    private IAlgorithm algorithm;
40    private AutoResetEvent waitHandle = new AutoResetEvent(false);
41    private CancellationToken cancellationToken;
42
43    public AlgorithmExecutor(IAlgorithm algorithm, CancellationToken cancellationToken) {
44      this.algorithm = algorithm;
45      this.cancellationToken = cancellationToken;
46    }
47
48    public void StartSync() {
49      algorithm.Stopped += new EventHandler(algorithm_Stopped);
50      algorithm.Paused += new EventHandler(algorithm_Paused);
51
52      using (CancellationTokenRegistration registration = cancellationToken.Register(new Action(cancellationToken_Canceled))) {
53        algorithm.Start();
54        waitHandle.WaitOne();
55        waitHandle.Dispose();
56      }
57
58      algorithm.Stopped -= new EventHandler(algorithm_Stopped);
59      algorithm.Paused -= new EventHandler(algorithm_Paused);
60      if (algorithm.ExecutionState == ExecutionState.Started) {
61        algorithm.Pause();
62      }
63      cancellationToken.ThrowIfCancellationRequested();
64    }
65
66    private void algorithm_Paused(object sender, EventArgs e) {
67      waitHandle.Set();
68    }
69
70    private void algorithm_Stopped(object sender, EventArgs e) {
71      waitHandle.Set();
72    }
73
74    private void cancellationToken_Canceled() {
75      waitHandle.Set();
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.