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
Location:
branches/Async/HeuristicLab.Core/3.3
Files:
3 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();
  • branches/Async/HeuristicLab.Core/3.3/Executable.cs

    r13354 r15065  
    8484      ExecutionTime = TimeSpan.Zero;
    8585    }
    86     public void Start() {
    87       StartAsync().Wait();
     86    public virtual void Start() {
     87      Start(CancellationToken.None);
    8888    }
    89     public async Task StartAsync() {
    90       await StartAsync(CancellationToken.None);
    91     }
    92     public virtual async Task StartAsync(CancellationToken cancellationToken) {
     89    public virtual void Start(CancellationToken cancellationToken) {
    9390      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
    9491        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
     92    }
     93    public virtual async Task StartAsync() { await StartAsync(CancellationToken.None); }
     94    public virtual async Task StartAsync(CancellationToken cancellationToken) {
     95      await Task.Factory.StartNew((ct) => Start((CancellationToken)ct), cancellationToken, cancellationToken);
    9596    }
    9697    public virtual void Pause() {
  • branches/Async/HeuristicLab.Core/3.3/Interfaces/IExecutable.cs

    r13349 r15065  
    3232    void Prepare();
    3333    void Start();
     34    void Start(CancellationToken cancellationToken);
    3435    Task StartAsync();
    3536    Task StartAsync(CancellationToken cancellationToken);
Note: See TracChangeset for help on using the changeset viewer.