Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/22/16 15:47:00 (8 years ago)
Author:
mkommend
Message:

#2524:

  • Renamed pausable to SupportsPause
  • Changed SupportsPause field to abstract property that has to be implemented
  • Stored initialization flag in BasicAlgorithm
  • Changed CancellationToken access to use the according property
  • Adapted HillClimber to new pausing mechanism
  • Disable pause for PPP, because it does not work correctly
  • Derived FixedDataAnalysisAlgorithm from BasicAlgorithm
  • Changed base class of all data analysis algorithm from BasicAlgorithm to FixedDataAnalysisAlgorithm
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs

    r14517 r14523  
    3030  [StorableClass]
    3131  public abstract class BasicAlgorithm : Algorithm, IStorableContent {
    32     private bool initialized;
     32
    3333    private bool pausePending;
    3434    private DateTime lastUpdateTime;
    35     protected bool pausable;
    3635
    3736    public string Filename { get; set; }
    38     public bool Pausable { get { return pausable; } }
     37
     38    public abstract bool SupportsPause { get; }
    3939
    4040    [Storable]
    41     private ResultCollection results;
     41    private bool initialized;
     42    [Storable]
     43    private readonly ResultCollection results;
    4244    public override ResultCollection Results {
    4345      get { return results; }
     
    5456    protected BasicAlgorithm(BasicAlgorithm original, Cloner cloner)
    5557      : base(original, cloner) {
    56       pausable = original.pausable;
    5758      results = cloner.Clone(original.Results);
    5859    }
     
    7576      pausePending = false;
    7677      OnStarted();
    77       Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
     78
     79      Task task = Task.Factory.StartNew(Run, CancellationTokenSource.Token, CancellationTokenSource.Token);
    7880      task.ContinueWith(t => {
    7981        try {
    8082          t.Wait();
    81         } catch (AggregateException ex) {
     83        }
     84        catch (AggregateException ex) {
    8285          try {
    8386            ex.Flatten().Handle(x => x is OperationCanceledException);
    84           } catch (AggregateException remaining) {
     87          }
     88          catch (AggregateException remaining) {
    8589            if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
    8690            else OnExceptionOccurred(remaining);
     
    9599
    96100    public override void Pause() {
    97       if (!Pausable)
     101      // CancellationToken.ThrowIfCancellationRequested() must be called from within the Run method, otherwise pause does nothing
     102      // alternatively check the IsCancellationRequested property of the cancellation token
     103      if (!SupportsPause)
    98104        throw new NotSupportedException("Pause is not supported by this algorithm.");
    99105
    100106      base.Pause();
    101107      pausePending = true;
    102       cancellationTokenSource.Cancel();
     108      CancellationTokenSource.Cancel();
    103109    }
    104110
     
    123129        initialized = true;
    124130        Run(cancellationToken);
    125       } finally {
     131      }
     132      finally {
    126133        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
    127134        timer.Stop();
Note: See TracChangeset for help on using the changeset viewer.