Changeset 15215 for branches/Async/HeuristicLab.Optimization/3.3/Algorithms
- Timestamp:
- 07/12/17 16:50:40 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Async/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r15065 r15215 22 22 using System; 23 23 using System.Threading; 24 using System.Threading.Tasks;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 61 60 } 62 61 63 public override void Start( ) {64 base.Start( );65 CancellationTokenSource = new CancellationTokenSource();62 public override void Start(CancellationToken cancellationToken) { 63 base.Start(cancellationToken); 64 CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); 66 65 67 66 OnStarted(); 68 Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token); 69 task.ContinueWith(t => { 70 try { 71 t.Wait(); 72 } catch (AggregateException ex) { 73 try { 74 ex.Flatten().Handle(x => x is OperationCanceledException); 75 } catch (AggregateException remaining) { 76 if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]); 77 else OnExceptionOccurred(remaining); 78 } 79 } 80 CancellationTokenSource.Dispose(); 81 CancellationTokenSource = null; 82 OnStopped(); 83 }); 67 try { 68 Run((object)cancellationTokenSource.Token); 69 } catch (OperationCanceledException) { 70 } catch (AggregateException ae) { 71 if (ae.InnerExceptions.Count == 1) OnExceptionOccurred(ae.InnerExceptions[0]); 72 else OnExceptionOccurred(ae); 73 } catch (Exception e) { 74 OnExceptionOccurred(e); 75 } 76 77 CancellationTokenSource.Dispose(); 78 CancellationTokenSource = null; 79 OnStopped(); 84 80 } 85 81
Note: See TracChangeset
for help on using the changeset viewer.