Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Tests/HeuristicLab-3.3/AlgorithmExtensions.cs @ 9793

Last change on this file since 9793 was 7915, checked in by mkommend, 12 years ago

#1777: Extracted unit tests into a separate solution.

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Common;
25using HeuristicLab.Core;
26
27namespace HeuristicLab_33.Tests {
28  public static class AlgorithmExtensions {
29    public static void StartSync(this IExecutable executable, CancellationToken cancellationToken) {
30      var executor = new AlgorithmExecutor(executable, cancellationToken);
31      executor.StartSync();
32    }
33  }
34
35  /// <summary>
36  /// Can execute an algorithm synchronously
37  /// </summary>
38  internal class AlgorithmExecutor {
39    private IExecutable executable;
40    private AutoResetEvent waitHandle = new AutoResetEvent(false);
41    private CancellationToken cancellationToken;
42    private Exception occuredException;
43
44    public AlgorithmExecutor(IExecutable executable, CancellationToken cancellationToken) {
45      this.executable = executable;
46      this.cancellationToken = cancellationToken;
47      this.occuredException = null;
48    }
49
50    public void StartSync() {
51      executable.Stopped += new EventHandler(executable_Stopped);
52      executable.Paused += new EventHandler(executable_Paused);
53      executable.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(executable_ExceptionOccurred);
54
55      using (CancellationTokenRegistration registration = cancellationToken.Register(new Action(cancellationToken_Canceled))) {
56        executable.Start();
57        waitHandle.WaitOne(-1, false);
58        waitHandle.Dispose();
59      }
60
61      executable.Stopped -= new EventHandler(executable_Stopped);
62      executable.Paused -= new EventHandler(executable_Paused);
63      if (executable.ExecutionState == ExecutionState.Started) {
64        executable.Pause();
65      }
66      cancellationToken.ThrowIfCancellationRequested();
67      if (occuredException != null) throw occuredException;
68    }
69
70    private void executable_Paused(object sender, EventArgs e) {
71      waitHandle.Set();
72    }
73
74    private void executable_Stopped(object sender, EventArgs e) {
75      waitHandle.Set();
76    }
77
78    private void executable_ExceptionOccurred(object sender, EventArgs<Exception> e) {
79      occuredException = e.Value; // after an exception occured the executable pauses
80    }
81
82    private void cancellationToken_Canceled() {
83      waitHandle.Set();
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.