Free cookie consent management tool by TermsFeed Policy Generator

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

#2258: added StartAsync to IExecutable

Location:
branches/Async/HeuristicLab.Optimization/3.3/Algorithms
Files:
3 edited

Legend:

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

    r12012 r13349  
    2424using System.Drawing;
    2525using System.Linq;
     26using System.Threading;
     27using System.Threading.Tasks;
    2628using HeuristicLab.Collections;
    2729using HeuristicLab.Common;
     
    202204      Prepare();
    203205    }
    204     public virtual void Start() {
     206    public void Start() {
     207      StartAsync().Wait();
     208    }
     209    public async Task StartAsync() {
     210      await StartAsync(new CancellationToken());
     211    }
     212    public virtual async Task StartAsync(CancellationToken cancellationToken) {
    205213      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
    206214        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
  • branches/Async/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs

    r11878 r13349  
    6161    }
    6262
    63     public override void Start() {
    64       base.Start();
     63    public override async Task StartAsync(CancellationToken cancellationToken) {
     64      await base.StartAsync(cancellationToken);
    6565      CancellationTokenSource = new CancellationTokenSource();
    6666
    6767      OnStarted();
    68       Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
    69       task.ContinueWith(t => {
    70         try {
    71           t.Wait();
    72         } catch (AggregateException ex) {
     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 => {
    7371          try {
    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);
     72            t.Wait();
    7873          }
    79         }
    80         CancellationTokenSource.Dispose();
    81         CancellationTokenSource = null;
    82         OnStopped();
    83       });
     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      }
    8488    }
    8589
     
    106110      try {
    107111        Run(cancellationToken);
    108       } finally {
     112      }
     113      finally {
    109114        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
    110115        timer.Stop();
  • branches/Async/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs

    r12012 r13349  
    2222using System;
    2323using System.Linq;
     24using System.Threading;
     25using System.Threading.Tasks;
    2426using HeuristicLab.Common;
    2527using HeuristicLab.Core;
    2628using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2729using HeuristicLab.PluginInfrastructure;
     30using ExecutionContext = HeuristicLab.Core.ExecutionContext;
    2831
    2932namespace HeuristicLab.Optimization {
     
    168171      }
    169172    }
    170     public override void Start() {
    171       base.Start();
    172       if (engine != null) engine.Start();
     173    public override async Task StartAsync(CancellationToken cancellationToken) {
     174      await base.StartAsync(cancellationToken);
     175      if (engine != null) await engine.StartAsync(cancellationToken);
    173176    }
    174177    public override void Pause() {
Note: See TracChangeset for help on using the changeset viewer.