Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/22/16 10:07:46 (8 years ago)
Author:
jkarder
Message:

#2524: made BasicAlgorithm pausable

File:
1 edited

Legend:

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

    r14185 r14517  
    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    private bool initialized;
     33    private bool pausePending;
     34    private DateTime lastUpdateTime;
     35    protected bool pausable;
     36
    3137    public string Filename { get; set; }
     38    public bool Pausable { get { return pausable; } }
    3239
    3340    [Storable]
     
    4754    protected BasicAlgorithm(BasicAlgorithm original, Cloner cloner)
    4855      : base(original, cloner) {
     56      pausable = original.pausable;
    4957      results = cloner.Clone(original.Results);
    5058    }
     
    5866      base.Prepare();
    5967      results.Clear();
     68      initialized = false;
    6069      OnPrepared();
    6170    }
     
    6473      base.Start();
    6574      CancellationTokenSource = new CancellationTokenSource();
    66 
     75      pausePending = false;
    6776      OnStarted();
    6877      Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
     
    8089        CancellationTokenSource.Dispose();
    8190        CancellationTokenSource = null;
    82         OnStopped();
     91        if (pausePending) OnPaused();
     92        else OnStopped();
    8393      });
    8494    }
    8595
    8696    public override void Pause() {
    87       throw new NotSupportedException("Pause is not supported in basic algorithms.");
     97      if (!Pausable)
     98        throw new NotSupportedException("Pause is not supported by this algorithm.");
     99
     100      base.Pause();
     101      pausePending = true;
     102      cancellationTokenSource.Cancel();
    88103    }
    89104
     
    92107      // alternatively check the IsCancellationRequested property of the cancellation token
    93108      base.Stop();
    94       CancellationTokenSource.Cancel();
     109      if (ExecutionState == ExecutionState.Paused) OnStopped();
     110      else CancellationTokenSource.Cancel();
    95111    }
    96112
    97 
    98     private DateTime lastUpdateTime;
    99113    private void Run(object state) {
    100114      CancellationToken cancellationToken = (CancellationToken)state;
     
    105119      timer.Start();
    106120      try {
     121        if (!initialized)
     122          Initialize(cancellationToken);
     123        initialized = true;
    107124        Run(cancellationToken);
    108125      } finally {
     
    113130    }
    114131
     132    protected virtual void Initialize(CancellationToken cancellationToken) { }
    115133    protected abstract void Run(CancellationToken cancellationToken);
    116134
Note: See TracChangeset for help on using the changeset viewer.