Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/28/11 23:29:26 (13 years ago)
Author:
cneumuel
Message:

#1569 checked in test-case

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab/3.3/Tests/AlgorithmExtensions.cs

    r6205 r6495  
    2222using System;
    2323using System.Threading;
     24using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    25 using HeuristicLab.Optimization;
    2626
    2727namespace HeuristicLab_33.Tests {
    2828  public static class AlgorithmExtensions {
    29     public static void StartSync(this IAlgorithm algorithm, CancellationToken cancellationToken) {
    30       var executor = new AlgorithmExecutor(algorithm, cancellationToken);
     29    public static void StartSync(this IExecutable executable, CancellationToken cancellationToken) {
     30      var executor = new AlgorithmExecutor(executable, cancellationToken);
    3131      executor.StartSync();
    3232    }
     
    3737  /// </summary>
    3838  internal class AlgorithmExecutor {
    39     private IAlgorithm algorithm;
     39    private IExecutable executable;
    4040    private AutoResetEvent waitHandle = new AutoResetEvent(false);
    4141    private CancellationToken cancellationToken;
     42    private Exception occuredException;
    4243
    43     public AlgorithmExecutor(IAlgorithm algorithm, CancellationToken cancellationToken) {
    44       this.algorithm = algorithm;
     44    public AlgorithmExecutor(IExecutable executable, CancellationToken cancellationToken) {
     45      this.executable = executable;
    4546      this.cancellationToken = cancellationToken;
     47      this.occuredException = null;
    4648    }
    4749
    4850    public void StartSync() {
    49       algorithm.Stopped += new EventHandler(algorithm_Stopped);
    50       algorithm.Paused += new EventHandler(algorithm_Paused);
     51      executable.Stopped += new EventHandler(executable_Stopped);
     52      executable.Paused += new EventHandler(executable_Paused);
     53      executable.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(executable_ExceptionOccurred);
    5154
    5255      using (CancellationTokenRegistration registration = cancellationToken.Register(new Action(cancellationToken_Canceled))) {
    53         algorithm.Start();
    54         waitHandle.WaitOne();
     56        executable.Start();
     57        waitHandle.WaitOne(-1, false);
    5558        waitHandle.Dispose();
    5659      }
    5760
    58       algorithm.Stopped -= new EventHandler(algorithm_Stopped);
    59       algorithm.Paused -= new EventHandler(algorithm_Paused);
    60       if (algorithm.ExecutionState == ExecutionState.Started) {
    61         algorithm.Pause();
     61      executable.Stopped -= new EventHandler(executable_Stopped);
     62      executable.Paused -= new EventHandler(executable_Paused);
     63      if (executable.ExecutionState == ExecutionState.Started) {
     64        executable.Pause();
    6265      }
    6366      cancellationToken.ThrowIfCancellationRequested();
     67      if (occuredException != null) throw occuredException;
    6468    }
    6569
    66     private void algorithm_Paused(object sender, EventArgs e) {
     70    private void executable_Paused(object sender, EventArgs e) {
    6771      waitHandle.Set();
    6872    }
    6973
    70     private void algorithm_Stopped(object sender, EventArgs e) {
     74    private void executable_Stopped(object sender, EventArgs e) {
    7175      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
    7280    }
    7381
Note: See TracChangeset for help on using the changeset viewer.