Changeset 14600 for branches/PerformanceComparison/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
- Timestamp:
- 01/23/17 16:11:02 (8 years ago)
- Location:
- branches/PerformanceComparison/HeuristicLab.Optimization
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.Optimization
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Optimization merged: 14071,14100,14136,14171,14185,14195,14433,14475,14517,14523
- Property svn:mergeinfo changed
-
branches/PerformanceComparison/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r12803 r14600 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 5Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 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 33 private bool pausePending; 34 private DateTime lastUpdateTime; 35 31 36 public string Filename { get; set; } 32 37 38 public abstract bool SupportsPause { get; } 39 33 40 [Storable] 34 private ResultCollection results; 41 private bool initialized; 42 [Storable] 43 private readonly ResultCollection results; 35 44 public override ResultCollection Results { 36 45 get { return results; } … … 58 67 base.Prepare(); 59 68 results.Clear(); 69 initialized = false; 60 70 OnPrepared(); 61 71 } … … 64 74 base.Start(); 65 75 CancellationTokenSource = new CancellationTokenSource(); 76 pausePending = false; 77 OnStarted(); 66 78 67 OnStarted(); 68 Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token); 79 Task task = Task.Factory.StartNew(Run, CancellationTokenSource.Token, CancellationTokenSource.Token); 69 80 task.ContinueWith(t => { 70 81 try { 71 82 t.Wait(); 72 } catch (AggregateException ex) { 83 } 84 catch (AggregateException ex) { 73 85 try { 74 86 ex.Flatten().Handle(x => x is OperationCanceledException); 75 } catch (AggregateException remaining) { 87 } 88 catch (AggregateException remaining) { 76 89 if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]); 77 90 else OnExceptionOccurred(remaining); … … 80 93 CancellationTokenSource.Dispose(); 81 94 CancellationTokenSource = null; 82 OnStopped(); 95 if (pausePending) OnPaused(); 96 else OnStopped(); 83 97 }); 84 98 } 85 99 86 100 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(); 88 109 } 89 110 … … 92 113 // alternatively check the IsCancellationRequested property of the cancellation token 93 114 base.Stop(); 94 CancellationTokenSource.Cancel(); 115 if (ExecutionState == ExecutionState.Paused) OnStopped(); 116 else CancellationTokenSource.Cancel(); 95 117 } 96 118 97 98 private DateTime lastUpdateTime;99 119 private void Run(object state) { 100 120 CancellationToken cancellationToken = (CancellationToken)state; … … 105 125 timer.Start(); 106 126 try { 127 if (!initialized) 128 Initialize(cancellationToken); 129 initialized = true; 107 130 Run(cancellationToken); 108 } finally { 131 } 132 finally { 109 133 timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed); 110 134 timer.Stop(); … … 113 137 } 114 138 139 protected virtual void Initialize(CancellationToken cancellationToken) { } 115 140 protected abstract void Run(CancellationToken cancellationToken); 116 141
Note: See TracChangeset
for help on using the changeset viewer.