- Timestamp:
- 07/23/17 11:17:18 (7 years ago)
- Location:
- branches/Async
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Async
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/Async/HeuristicLab.Optimization
- Property svn:mergeinfo changed
-
branches/Async/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r15232 r15281 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; 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(cancellationToken); 65 75 CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); 66 76 pausePending = false; 67 77 OnStarted(); 68 78 try { … … 81 91 82 92 public override void Pause() { 83 throw new NotSupportedException("Pause is not supported in basic algorithms."); 93 // CancellationToken.ThrowIfCancellationRequested() must be called from within the Run method, otherwise pause does nothing 94 // alternatively check the IsCancellationRequested property of the cancellation token 95 if (!SupportsPause) 96 throw new NotSupportedException("Pause is not supported by this algorithm."); 97 98 base.Pause(); 99 pausePending = true; 100 CancellationTokenSource.Cancel(); 84 101 } 85 102 … … 88 105 // alternatively check the IsCancellationRequested property of the cancellation token 89 106 base.Stop(); 90 CancellationTokenSource.Cancel(); 107 if (ExecutionState == ExecutionState.Paused) OnStopped(); 108 else CancellationTokenSource.Cancel(); 91 109 } 92 110 93 94 private DateTime lastUpdateTime;95 111 private void Run(object state) { 96 112 CancellationToken cancellationToken = (CancellationToken)state; … … 101 117 timer.Start(); 102 118 try { 119 if (!initialized) 120 Initialize(cancellationToken); 121 initialized = true; 103 122 Run(cancellationToken); 104 } finally { 123 } 124 finally { 105 125 timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed); 106 126 timer.Stop(); … … 109 129 } 110 130 131 protected virtual void Initialize(CancellationToken cancellationToken) { } 111 132 protected abstract void Run(CancellationToken cancellationToken); 112 133
Note: See TracChangeset
for help on using the changeset viewer.