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.Optimization/3.3/Algorithms
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/Async/HeuristicLab.Optimization/3.3/Algorithms/Algorithm.cs

    r13354 r15065  
    204204      Prepare();
    205205    }
    206     public void Start() {
    207       StartAsync().Wait();
    208     }
    209     public async Task StartAsync() {
    210       await StartAsync(CancellationToken.None);
    211     }
    212     public virtual async Task StartAsync(CancellationToken cancellationToken) {
     206    public virtual void Start() {
     207      Start(CancellationToken.None);
     208    }
     209    public virtual void Start(CancellationToken cancellationToken) {
    213210      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
    214211        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
     212    }
     213    public virtual async Task StartAsync() { await StartAsync(CancellationToken.None); }
     214    public virtual async Task StartAsync(CancellationToken cancellationToken) {
     215      await Task.Factory.StartNew((ct) => Start((CancellationToken)ct), cancellationToken, cancellationToken);
    215216    }
    216217    public virtual void Pause() {
  • branches/Async/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs

    r13349 r15065  
    6161    }
    6262
    63     public override async Task StartAsync(CancellationToken cancellationToken) {
    64       await base.StartAsync(cancellationToken);
     63    public override void Start() {
     64      base.Start();
    6565      CancellationTokenSource = new CancellationTokenSource();
    6666
    6767      OnStarted();
    68       using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationTokenSource.Token, cancellationToken)) {
    69         Task task = Task.Factory.StartNew(Run, cts.Token, cts.Token);
    70         await task.ContinueWith(t => {
     68      Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
     69      task.ContinueWith(t => {
     70        try {
     71          t.Wait();
     72        } catch (AggregateException ex) {
    7173          try {
    72             t.Wait();
     74            ex.Flatten().Handle(x => x is OperationCanceledException);
     75          } catch (AggregateException remaining) {
     76            if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
     77            else OnExceptionOccurred(remaining);
    7378          }
    74           catch (AggregateException ex) {
    75             try {
    76               ex.Flatten().Handle(x => x is OperationCanceledException);
    77             }
    78             catch (AggregateException remaining) {
    79               if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
    80               else OnExceptionOccurred(remaining);
    81             }
    82           }
    83           CancellationTokenSource.Dispose();
    84           CancellationTokenSource = null;
    85           OnStopped();
    86         });
    87       }
     79        }
     80        CancellationTokenSource.Dispose();
     81        CancellationTokenSource = null;
     82        OnStopped();
     83      });
    8884    }
    8985
     
    110106      try {
    111107        Run(cancellationToken);
    112       }
    113       finally {
     108      } finally {
    114109        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
    115110        timer.Stop();
  • branches/Async/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs

    r13349 r15065  
    2222using System;
    2323using System.Linq;
    24 using System.Threading;
    25 using System.Threading.Tasks;
    2624using HeuristicLab.Common;
    2725using HeuristicLab.Core;
    2826using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2927using HeuristicLab.PluginInfrastructure;
    30 using ExecutionContext = HeuristicLab.Core.ExecutionContext;
    3128
    3229namespace HeuristicLab.Optimization {
     
    171168      }
    172169    }
    173     public override async Task StartAsync(CancellationToken cancellationToken) {
    174       await base.StartAsync(cancellationToken);
    175       if (engine != null) await engine.StartAsync(cancellationToken);
     170    public override void Start(System.Threading.CancellationToken cancellationToken) {
     171      base.Start(cancellationToken);
     172      if (engine != null) engine.Start(cancellationToken);
    176173    }
    177174    public override void Pause() {
Note: See TracChangeset for help on using the changeset viewer.