Ignore:
Timestamp:
06/26/17 09:45:36 (2 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.Core/3.3/Engine.cs

    r13349 r15065  
    2323using System.Collections.Generic;
    2424using System.Threading;
    25 using System.Threading.Tasks;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    8281    }
    8382
    84     public override async Task StartAsync(CancellationToken cancellationToken) {
    85       await base.StartAsync(cancellationToken);
    86       cancellationTokenSource = new CancellationTokenSource();
     83    public override void Start(CancellationToken cancellationToken) {
     84      base.Start(cancellationToken);
     85      cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
    8786      stopPending = false;
    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 => {
    91           try {
    92             t.Wait();
    93           }
    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             }
    102           }
    103           cancellationTokenSource.Dispose();
    104           cancellationTokenSource = null;
    105           if (stopPending) executionStack.Clear();
    106           if (executionStack.Count == 0) OnStopped();
    107           else OnPaused();
    108         });
     87
     88      try {
     89        Run((object)cancellationTokenSource.Token);
     90      } catch (OperationCanceledException) {
     91      } catch (AggregateException ae) {
     92        if (ae.InnerExceptions.Count == 1) OnExceptionOccurred(ae.InnerExceptions[0]);
     93        else OnExceptionOccurred(ae);
     94      } catch (Exception e) {
     95        OnExceptionOccurred(e);
    10996      }
     97
     98      cancellationTokenSource.Dispose();
     99      cancellationTokenSource = null;
     100      if (stopPending) executionStack.Clear();
     101      if (executionStack.Count == 0) OnStopped();
     102      else OnPaused();
    110103    }
    111104    protected override void OnStarted() {
     
    154147      try {
    155148        Run(cancellationToken);
    156       }
    157       finally {
     149      } finally {
    158150        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
    159151        timer.Stop();
Note: See TracChangeset for help on using the changeset viewer.