Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/28/17 16:47:42 (7 years ago)
Author:
jkarder
Message:

#2258: merged r15287 into stable

Location:
stable
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Core

  • stable/HeuristicLab.Core/3.3/Engine.cs

    r14186 r15292  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using System.Threading;
    25 using System.Threading.Tasks;
    2626using HeuristicLab.Common;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    8282    }
    8383
    84     public override void Start() {
    85       base.Start();
    86       cancellationTokenSource = new CancellationTokenSource();
     84    public override void Start(CancellationToken cancellationToken) {
     85      base.Start(cancellationToken);
     86      cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
    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) {
    94           try {
    95             ex.Flatten().Handle(x => x is OperationCanceledException);
    96           }
    97           catch (AggregateException remaining) {
    98             if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
    99             else OnExceptionOccurred(remaining);
    100           }
    101         }
    102         cancellationTokenSource.Dispose();
    103         cancellationTokenSource = null;
    104         if (stopPending) executionStack.Clear();
    105         if (executionStack.Count == 0) OnStopped();
    106         else OnPaused();
    107       });
     88
     89      try {
     90        Run((object)cancellationTokenSource.Token);
     91      } catch (OperationCanceledException) {
     92      } catch (AggregateException ae) {
     93        OnExceptionOccurred(ae.InnerExceptions.SingleOrDefault() ?? ae);
     94      } catch (Exception e) {
     95        OnExceptionOccurred(e);
     96      }
     97
     98      cancellationTokenSource.Dispose();
     99      cancellationTokenSource = null;
     100      if (stopPending) executionStack.Clear();
     101      if (executionStack.Count == 0) OnStopped();
     102      else OnPaused();
    108103    }
    109104    protected override void OnStarted() {
     
    152147      try {
    153148        Run(cancellationToken);
    154       }
    155       finally {
     149      } finally {
    156150        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
    157151        timer.Stop();
  • stable/HeuristicLab.Core/3.3/Executable.cs

    r14186 r15292  
    2222using System;
    2323using System.Drawing;
     24using System.Threading;
     25using System.Threading.Tasks;
    2426using HeuristicLab.Common;
    2527using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    8385    }
    8486    public virtual void Start() {
     87      Start(CancellationToken.None);
     88    }
     89    public virtual void Start(CancellationToken cancellationToken) {
    8590      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
    8691        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 AsyncHelper.DoAsync(Start, cancellationToken);
    8796    }
    8897    public virtual void Pause() {
  • stable/HeuristicLab.Core/3.3/Interfaces/IExecutable.cs

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