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

#2258: added StartAsync to IExecutable

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Async/HeuristicLab.Core/3.3/Engine.cs

    r12012 r13349  
    8282    }
    8383
    84     public override void Start() {
    85       base.Start();
     84    public override async Task StartAsync(CancellationToken cancellationToken) {
     85      await base.StartAsync(cancellationToken);
    8686      cancellationTokenSource = new CancellationTokenSource();
    8787      stopPending = false;
    88       Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
    89       task.ContinueWith(t => {
    90         try {
    91           t.Wait();
    92         }
    93         catch (AggregateException ex) {
     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 => {
    9491          try {
    95             ex.Flatten().Handle(x => x is OperationCanceledException);
     92            t.Wait();
    9693          }
    97           catch (AggregateException remaining) {
    98             if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
    99             else OnExceptionOccurred(remaining);
     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            }
    100102          }
    101         }
    102         cancellationTokenSource.Dispose();
    103         cancellationTokenSource = null;
    104         if (stopPending) executionStack.Clear();
    105         if (executionStack.Count == 0) OnStopped();
    106         else OnPaused();
    107       });
     103          cancellationTokenSource.Dispose();
     104          cancellationTokenSource = null;
     105          if (stopPending) executionStack.Clear();
     106          if (executionStack.Count == 0) OnStopped();
     107          else OnPaused();
     108        });
     109      }
    108110    }
    109111    protected override void OnStarted() {
Note: See TracChangeset for help on using the changeset viewer.