Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/26/17 09:45:36 (7 years ago)
Author:
jkarder
Message:

#2258: refactored async methods

  • synchronously called IExecutables are now executed in the caller's thread
  • removed old synchronization code from unit tests
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Async/HeuristicLab.Algorithms.Benchmarks/3.3/BenchmarkAlgorithm.cs

    r13354 r15065  
    302302    }
    303303    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); }
    309325    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
    334329    private void Run(object state) {
    335330      CancellationToken cancellationToken = (CancellationToken)state;
     
    353348        Benchmark.TimeLimit = timelimit;
    354349        Benchmark.Run(cancellationToken, results);
    355       }
    356       catch (OperationCanceledException) {
    357       }
    358       finally {
     350      } catch (OperationCanceledException) {
     351      } finally {
    359352        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
    360353        timer.Stop();
Note: See TracChangeset for help on using the changeset viewer.