Changeset 15065 for branches/Async/HeuristicLab.Algorithms.Benchmarks/3.3
- Timestamp:
- 06/26/17 09:45:36 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Async/HeuristicLab.Algorithms.Benchmarks/3.3/BenchmarkAlgorithm.cs
r13354 r15065 302 302 } 303 303 public void Start() { 304 StartAsync().Wait(); 305 } 306 public async Task StartAsync() { 307 await StartAsync(CancellationToken.None); 308 } 304 Start(CancellationToken.None); 305 } 306 public void Start(CancellationToken cancellationToken) { 307 cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); 308 OnStarted(); 309 310 try { 311 Run(cancellationTokenSource.Token); 312 } catch (OperationCanceledException) { 313 } catch (AggregateException ae) { 314 if (ae.InnerExceptions.Count == 1) OnExceptionOccurred(ae.InnerExceptions[0]); 315 else OnExceptionOccurred(ae); 316 } catch (Exception e) { 317 OnExceptionOccurred(e); 318 } 319 320 cancellationTokenSource.Dispose(); 321 cancellationTokenSource = null; 322 OnStopped(); 323 } 324 public async Task StartAsync() { await StartAsync(CancellationToken.None); } 309 325 public async Task StartAsync(CancellationToken cancellationToken) { 310 cancellationTokenSource = new CancellationTokenSource(); 311 OnStarted(); 312 using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationTokenSource.Token, cancellationToken)) { 313 Task task = Task.Factory.StartNew(Run, cts.Token, cts.Token); 314 await task.ContinueWith(t => { 315 try { 316 t.Wait(); 317 } 318 catch (AggregateException ex) { 319 try { 320 ex.Flatten().Handle(x => x is OperationCanceledException); 321 } 322 catch (AggregateException remaining) { 323 if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]); 324 else OnExceptionOccurred(remaining); 325 } 326 } 327 328 cancellationTokenSource.Dispose(); 329 cancellationTokenSource = null; 330 OnStopped(); 331 }); 332 } 333 } 326 await Task.Factory.StartNew((ct) => Start((CancellationToken)ct), cancellationToken, cancellationToken); 327 } 328 334 329 private void Run(object state) { 335 330 CancellationToken cancellationToken = (CancellationToken)state; … … 353 348 Benchmark.TimeLimit = timelimit; 354 349 Benchmark.Run(cancellationToken, results); 355 } 356 catch (OperationCanceledException) { 357 } 358 finally { 350 } catch (OperationCanceledException) { 351 } finally { 359 352 timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed); 360 353 timer.Stop();
Note: See TracChangeset
for help on using the changeset viewer.