Changeset 15065 for branches/Async/HeuristicLab.Core/3.3
- Timestamp:
- 06/26/17 09:45:36 (7 years ago)
- Location:
- branches/Async/HeuristicLab.Core/3.3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Async/HeuristicLab.Core/3.3/Engine.cs
r13349 r15065 23 23 using System.Collections.Generic; 24 24 using System.Threading; 25 using System.Threading.Tasks;26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 82 81 } 83 82 84 public override async Task StartAsync(CancellationToken cancellationToken) {85 await base.StartAsync(cancellationToken);86 cancellationTokenSource = new CancellationTokenSource();83 public override void Start(CancellationToken cancellationToken) { 84 base.Start(cancellationToken); 85 cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); 87 86 stopPending = false; 88 using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationTokenSource.Token, cancellationToken)) { 89 Task task = Task.Factory.StartNew(Run, cts.Token, cts.Token); 90 await task.ContinueWith(t => { 91 try { 92 t.Wait(); 93 } 94 catch (AggregateException ex) { 95 try { 96 ex.Flatten().Handle(x => x is OperationCanceledException); 97 } 98 catch (AggregateException remaining) { 99 if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]); 100 else OnExceptionOccurred(remaining); 101 } 102 } 103 cancellationTokenSource.Dispose(); 104 cancellationTokenSource = null; 105 if (stopPending) executionStack.Clear(); 106 if (executionStack.Count == 0) OnStopped(); 107 else OnPaused(); 108 }); 87 88 try { 89 Run((object)cancellationTokenSource.Token); 90 } catch (OperationCanceledException) { 91 } catch (AggregateException ae) { 92 if (ae.InnerExceptions.Count == 1) OnExceptionOccurred(ae.InnerExceptions[0]); 93 else OnExceptionOccurred(ae); 94 } catch (Exception e) { 95 OnExceptionOccurred(e); 109 96 } 97 98 cancellationTokenSource.Dispose(); 99 cancellationTokenSource = null; 100 if (stopPending) executionStack.Clear(); 101 if (executionStack.Count == 0) OnStopped(); 102 else OnPaused(); 110 103 } 111 104 protected override void OnStarted() { … … 154 147 try { 155 148 Run(cancellationToken); 156 } 157 finally { 149 } finally { 158 150 timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed); 159 151 timer.Stop(); -
branches/Async/HeuristicLab.Core/3.3/Executable.cs
r13354 r15065 84 84 ExecutionTime = TimeSpan.Zero; 85 85 } 86 public v oid Start() {87 Start Async().Wait();86 public virtual void Start() { 87 Start(CancellationToken.None); 88 88 } 89 public async Task StartAsync() { 90 await StartAsync(CancellationToken.None); 91 } 92 public virtual async Task StartAsync(CancellationToken cancellationToken) { 89 public virtual void Start(CancellationToken cancellationToken) { 93 90 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 94 91 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 92 } 93 public virtual async Task StartAsync() { await StartAsync(CancellationToken.None); } 94 public virtual async Task StartAsync(CancellationToken cancellationToken) { 95 await Task.Factory.StartNew((ct) => Start((CancellationToken)ct), cancellationToken, cancellationToken); 95 96 } 96 97 public virtual void Pause() { -
branches/Async/HeuristicLab.Core/3.3/Interfaces/IExecutable.cs
r13349 r15065 32 32 void Prepare(); 33 33 void Start(); 34 void Start(CancellationToken cancellationToken); 34 35 Task StartAsync(); 35 36 Task StartAsync(CancellationToken cancellationToken);
Note: See TracChangeset
for help on using the changeset viewer.