Changeset 14523 for trunk/sources/HeuristicLab.Optimization
- Timestamp:
- 12/22/16 15:47:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r14517 r14523 30 30 [StorableClass] 31 31 public abstract class BasicAlgorithm : Algorithm, IStorableContent { 32 private bool initialized; 32 33 33 private bool pausePending; 34 34 private DateTime lastUpdateTime; 35 protected bool pausable;36 35 37 36 public string Filename { get; set; } 38 public bool Pausable { get { return pausable; } } 37 38 public abstract bool SupportsPause { get; } 39 39 40 40 [Storable] 41 private ResultCollection results; 41 private bool initialized; 42 [Storable] 43 private readonly ResultCollection results; 42 44 public override ResultCollection Results { 43 45 get { return results; } … … 54 56 protected BasicAlgorithm(BasicAlgorithm original, Cloner cloner) 55 57 : base(original, cloner) { 56 pausable = original.pausable;57 58 results = cloner.Clone(original.Results); 58 59 } … … 75 76 pausePending = false; 76 77 OnStarted(); 77 Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token); 78 79 Task task = Task.Factory.StartNew(Run, CancellationTokenSource.Token, CancellationTokenSource.Token); 78 80 task.ContinueWith(t => { 79 81 try { 80 82 t.Wait(); 81 } catch (AggregateException ex) { 83 } 84 catch (AggregateException ex) { 82 85 try { 83 86 ex.Flatten().Handle(x => x is OperationCanceledException); 84 } catch (AggregateException remaining) { 87 } 88 catch (AggregateException remaining) { 85 89 if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]); 86 90 else OnExceptionOccurred(remaining); … … 95 99 96 100 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) 98 104 throw new NotSupportedException("Pause is not supported by this algorithm."); 99 105 100 106 base.Pause(); 101 107 pausePending = true; 102 cancellationTokenSource.Cancel();108 CancellationTokenSource.Cancel(); 103 109 } 104 110 … … 123 129 initialized = true; 124 130 Run(cancellationToken); 125 } finally { 131 } 132 finally { 126 133 timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed); 127 134 timer.Stop();
Note: See TracChangeset
for help on using the changeset viewer.