Changeset 14517 for trunk/sources/HeuristicLab.Optimization
- Timestamp:
- 12/22/16 10:07:46 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r14185 r14517 24 24 using System.Threading.Tasks; 25 25 using HeuristicLab.Common; 26 using HeuristicLab.Core; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 … … 29 30 [StorableClass] 30 31 public abstract class BasicAlgorithm : Algorithm, IStorableContent { 32 private bool initialized; 33 private bool pausePending; 34 private DateTime lastUpdateTime; 35 protected bool pausable; 36 31 37 public string Filename { get; set; } 38 public bool Pausable { get { return pausable; } } 32 39 33 40 [Storable] … … 47 54 protected BasicAlgorithm(BasicAlgorithm original, Cloner cloner) 48 55 : base(original, cloner) { 56 pausable = original.pausable; 49 57 results = cloner.Clone(original.Results); 50 58 } … … 58 66 base.Prepare(); 59 67 results.Clear(); 68 initialized = false; 60 69 OnPrepared(); 61 70 } … … 64 73 base.Start(); 65 74 CancellationTokenSource = new CancellationTokenSource(); 66 75 pausePending = false; 67 76 OnStarted(); 68 77 Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token); … … 80 89 CancellationTokenSource.Dispose(); 81 90 CancellationTokenSource = null; 82 OnStopped(); 91 if (pausePending) OnPaused(); 92 else OnStopped(); 83 93 }); 84 94 } 85 95 86 96 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(); 88 103 } 89 104 … … 92 107 // alternatively check the IsCancellationRequested property of the cancellation token 93 108 base.Stop(); 94 CancellationTokenSource.Cancel(); 109 if (ExecutionState == ExecutionState.Paused) OnStopped(); 110 else CancellationTokenSource.Cancel(); 95 111 } 96 112 97 98 private DateTime lastUpdateTime;99 113 private void Run(object state) { 100 114 CancellationToken cancellationToken = (CancellationToken)state; … … 105 119 timer.Start(); 106 120 try { 121 if (!initialized) 122 Initialize(cancellationToken); 123 initialized = true; 107 124 Run(cancellationToken); 108 125 } finally { … … 113 130 } 114 131 132 protected virtual void Initialize(CancellationToken cancellationToken) { } 115 133 protected abstract void Run(CancellationToken cancellationToken); 116 134
Note: See TracChangeset
for help on using the changeset viewer.