Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/23/17 16:11:02 (7 years ago)
Author:
abeham
Message:

#2457: updated branch to trunk

Location:
branches/PerformanceComparison/HeuristicLab.Optimization
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/PerformanceComparison/HeuristicLab.Optimization

  • branches/PerformanceComparison/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs

    r12803 r14600  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2424using System.Threading.Tasks;
    2525using HeuristicLab.Common;
     26using HeuristicLab.Core;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
     
    2930  [StorableClass]
    3031  public abstract class BasicAlgorithm : Algorithm, IStorableContent {
     32
     33    private bool pausePending;
     34    private DateTime lastUpdateTime;
     35
    3136    public string Filename { get; set; }
    3237
     38    public abstract bool SupportsPause { get; }
     39
    3340    [Storable]
    34     private ResultCollection results;
     41    private bool initialized;
     42    [Storable]
     43    private readonly ResultCollection results;
    3544    public override ResultCollection Results {
    3645      get { return results; }
     
    5867      base.Prepare();
    5968      results.Clear();
     69      initialized = false;
    6070      OnPrepared();
    6171    }
     
    6474      base.Start();
    6575      CancellationTokenSource = new CancellationTokenSource();
     76      pausePending = false;
     77      OnStarted();
    6678
    67       OnStarted();
    68       Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
     79      Task task = Task.Factory.StartNew(Run, CancellationTokenSource.Token, CancellationTokenSource.Token);
    6980      task.ContinueWith(t => {
    7081        try {
    7182          t.Wait();
    72         } catch (AggregateException ex) {
     83        }
     84        catch (AggregateException ex) {
    7385          try {
    7486            ex.Flatten().Handle(x => x is OperationCanceledException);
    75           } catch (AggregateException remaining) {
     87          }
     88          catch (AggregateException remaining) {
    7689            if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
    7790            else OnExceptionOccurred(remaining);
     
    8093        CancellationTokenSource.Dispose();
    8194        CancellationTokenSource = null;
    82         OnStopped();
     95        if (pausePending) OnPaused();
     96        else OnStopped();
    8397      });
    8498    }
    8599
    86100    public override void Pause() {
    87       throw new NotSupportedException("Pause is not supported in basic algorithms.");
     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)
     104        throw new NotSupportedException("Pause is not supported by this algorithm.");
     105
     106      base.Pause();
     107      pausePending = true;
     108      CancellationTokenSource.Cancel();
    88109    }
    89110
     
    92113      // alternatively check the IsCancellationRequested property of the cancellation token
    93114      base.Stop();
    94       CancellationTokenSource.Cancel();
     115      if (ExecutionState == ExecutionState.Paused) OnStopped();
     116      else CancellationTokenSource.Cancel();
    95117    }
    96118
    97 
    98     private DateTime lastUpdateTime;
    99119    private void Run(object state) {
    100120      CancellationToken cancellationToken = (CancellationToken)state;
     
    105125      timer.Start();
    106126      try {
     127        if (!initialized)
     128          Initialize(cancellationToken);
     129        initialized = true;
    107130        Run(cancellationToken);
    108       } finally {
     131      }
     132      finally {
    109133        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
    110134        timer.Stop();
     
    113137    }
    114138
     139    protected virtual void Initialize(CancellationToken cancellationToken) { }
    115140    protected abstract void Run(CancellationToken cancellationToken);
    116141
Note: See TracChangeset for help on using the changeset viewer.