Ignore:
Timestamp:
11/23/15 21:18:26 (4 years ago)
Author:
jkarder
Message:

#2258: added StartAsync to IExecutable

File:
1 edited

Legend:

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

    r12504 r13349  
    302302    }
    303303    public void Start() {
     304      StartAsync().Wait();
     305    }
     306    public async Task StartAsync() {
     307      await StartAsync(new CancellationToken());
     308    }
     309    public async Task StartAsync(CancellationToken cancellationToken) {
    304310      cancellationTokenSource = new CancellationTokenSource();
    305311      OnStarted();
    306       Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
    307       task.ContinueWith(t => {
    308         try {
    309           t.Wait();
    310         }
    311         catch (AggregateException ex) {
     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 => {
    312315          try {
    313             ex.Flatten().Handle(x => x is OperationCanceledException);
     316            t.Wait();
    314317          }
    315           catch (AggregateException remaining) {
    316             if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
    317             else OnExceptionOccurred(remaining);
     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            }
    318326          }
    319         }
    320 
    321         cancellationTokenSource.Dispose();
    322         cancellationTokenSource = null;
    323         OnStopped();
    324       });
    325     }
    326 
     327
     328          cancellationTokenSource.Dispose();
     329          cancellationTokenSource = null;
     330          OnStopped();
     331        });
     332      }
     333    }
    327334    private void Run(object state) {
    328335      CancellationToken cancellationToken = (CancellationToken)state;
Note: See TracChangeset for help on using the changeset viewer.