- Timestamp:
- 11/24/15 17:46:18 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PausableBasicAlgorithm/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r11878 r13378 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 … … 30 31 public abstract class BasicAlgorithm : Algorithm, IStorableContent { 31 32 public string Filename { get; set; } 33 public virtual bool Pausable { get { return false; } } 32 34 33 35 [Storable] … … 58 60 base.Prepare(); 59 61 results.Clear(); 62 initialized = false; 60 63 OnPrepared(); 61 64 } … … 64 67 base.Start(); 65 68 CancellationTokenSource = new CancellationTokenSource(); 66 69 pausePending = false; 67 70 OnStarted(); 68 71 Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token); … … 80 83 CancellationTokenSource.Dispose(); 81 84 CancellationTokenSource = null; 82 OnStopped(); 85 if (pausePending) OnPaused(); 86 else OnStopped(); 83 87 }); 84 88 } 85 89 90 private bool pausePending; 86 91 public override void Pause() { 87 throw new NotSupportedException("Pause is not supported in basic algorithms."); 92 if (!Pausable) 93 throw new NotSupportedException("Pause is not supported by this algorithm."); 94 95 base.Pause(); 96 pausePending = Pausable; 97 cancellationTokenSource.Cancel(); 88 98 } 89 99 … … 92 102 // alternatively check the IsCancellationRequested property of the cancellation token 93 103 base.Stop(); 94 CancellationTokenSource.Cancel(); 104 if (ExecutionState == ExecutionState.Paused) OnStopped(); 105 else CancellationTokenSource.Cancel(); 95 106 } 96 107 … … 105 116 timer.Start(); 106 117 try { 118 if (!initialized) 119 Initialize(cancellationToken); 120 initialized = true; 107 121 Run(cancellationToken); 108 122 } finally { … … 113 127 } 114 128 129 private bool initialized; 130 protected virtual void Initialize(CancellationToken cancellationToken) { } 115 131 protected abstract void Run(CancellationToken cancellationToken); 116 132
Note: See TracChangeset
for help on using the changeset viewer.