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.DebugEngine/3.3/DebugEngine.cs

    r13349 r15065  
    2323using System.Linq;
    2424using System.Threading;
    25 using System.Threading.Tasks;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Core;
     
    161160        while (skipStackOperations && !(CurrentOperation is IAtomicOperation) && CanContinue)
    162161          ProcessNextOperation(true, cancellationTokenSource.Token);
    163       }
    164       catch (Exception ex) {
     162      } catch (Exception ex) {
    165163        OnExceptionOccurred(ex);
    166164      }
     
    174172    }
    175173
    176     public override async Task StartAsync(CancellationToken cancellationToken) {
    177       await base.StartAsync(cancellationToken);
    178       cancellationTokenSource = new CancellationTokenSource();
     174    public override void Start(CancellationToken cancellationToken) {
     175      base.Start(cancellationToken);
     176      cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
    179177      stopPending = false;
    180       using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationTokenSource.Token, cancellationToken)) {
    181         Task task = Task.Factory.StartNew(Run, cts.Token, cts.Token);
    182         await task.ContinueWith(t => {
    183           try {
    184             t.Wait();
    185           }
    186           catch (AggregateException ex) {
    187             try {
    188               ex.Flatten().Handle(x => x is OperationCanceledException);
    189             }
    190             catch (AggregateException remaining) {
    191               if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
    192               else OnExceptionOccurred(remaining);
    193             }
    194           }
    195           cancellationTokenSource.Dispose();
    196           cancellationTokenSource = null;
    197 
    198           if (stopPending) ExecutionStack.Clear();
    199           if (stopPending || !CanContinue) OnStopped();
    200           else OnPaused();
    201         });
    202       }
    203     }
    204 
     178
     179      try {
     180        Run(cancellationTokenSource.Token);
     181      } catch (OperationCanceledException) {
     182      } catch (AggregateException ae) {
     183        if (ae.InnerExceptions.Count == 1) OnExceptionOccurred(ae.InnerExceptions[0]);
     184        else OnExceptionOccurred(ae);
     185      } catch (Exception e) {
     186        OnExceptionOccurred(e);
     187      }
     188
     189      cancellationTokenSource.Dispose();
     190      cancellationTokenSource = null;
     191      if (stopPending) ExecutionStack.Clear();
     192      if (stopPending || !CanContinue) OnStopped();
     193      else OnPaused();
     194    }
    205195    protected override void OnStarted() {
    206196      Log.LogMessage("Engine started");
     
    252242          ProcessNextOperation(false, cancellationToken);
    253243        cancellationToken.ThrowIfCancellationRequested();
    254       }
    255       finally {
     244      } finally {
    256245        timer.Stop();
    257246        ExecutionTime += DateTime.UtcNow - lastUpdateTime;
     
    319308          }
    320309          CurrentOperation = null;
    321         }
    322         catch (Exception ex) {
     310        } catch (Exception ex) {
    323311          if (ex is OperationCanceledException) throw ex;
    324312          else throw new OperatorExecutionException(operation.Operator, ex);
Note: See TracChangeset for help on using the changeset viewer.