Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12337 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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