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

Legend:

Unmodified
Added
Removed
  • branches/Async/HeuristicLab.Core/3.3/Engine.cs

    r12012 r13349  
    8282    }
    8383
    84     public override void Start() {
    85       base.Start();
     84    public override async Task StartAsync(CancellationToken cancellationToken) {
     85      await base.StartAsync(cancellationToken);
    8686      cancellationTokenSource = new CancellationTokenSource();
    8787      stopPending = false;
    88       Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
    89       task.ContinueWith(t => {
    90         try {
    91           t.Wait();
    92         }
    93         catch (AggregateException ex) {
     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 => {
    9491          try {
    95             ex.Flatten().Handle(x => x is OperationCanceledException);
     92            t.Wait();
    9693          }
    97           catch (AggregateException remaining) {
    98             if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
    99             else OnExceptionOccurred(remaining);
     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            }
    100102          }
    101         }
    102         cancellationTokenSource.Dispose();
    103         cancellationTokenSource = null;
    104         if (stopPending) executionStack.Clear();
    105         if (executionStack.Count == 0) OnStopped();
    106         else OnPaused();
    107       });
     103          cancellationTokenSource.Dispose();
     104          cancellationTokenSource = null;
     105          if (stopPending) executionStack.Clear();
     106          if (executionStack.Count == 0) OnStopped();
     107          else OnPaused();
     108        });
     109      }
    108110    }
    109111    protected override void OnStarted() {
  • branches/Async/HeuristicLab.Core/3.3/Executable.cs

    r12012 r13349  
    2222using System;
    2323using System.Drawing;
     24using System.Threading;
     25using System.Threading.Tasks;
    2426using HeuristicLab.Common;
    2527using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    8284      ExecutionTime = TimeSpan.Zero;
    8385    }
    84     public virtual void Start() {
     86    public void Start() {
     87      StartAsync().Wait();
     88    }
     89    public async Task StartAsync() {
     90      await StartAsync(new CancellationToken());
     91    }
     92    public virtual async Task StartAsync(CancellationToken cancellationToken) {
    8593      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
    8694        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
  • branches/Async/HeuristicLab.Core/3.3/Interfaces/IExecutable.cs

    r12012 r13349  
    2121
    2222using System;
     23using System.Threading;
     24using System.Threading.Tasks;
    2325using HeuristicLab.Common;
    2426
     
    3032    void Prepare();
    3133    void Start();
     34    Task StartAsync();
     35    Task StartAsync(CancellationToken cancellationToken);
    3236    void Pause();
    3337    void Stop();
Note: See TracChangeset for help on using the changeset viewer.