Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/26/17 15:48:32 (7 years ago)
Author:
jkarder
Message:

#2258: merged Async branch into trunk

Location:
trunk/sources
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources

  • trunk/sources/HeuristicLab.Core

  • trunk/sources/HeuristicLab.Core/3.3/Engine.cs

    r14185 r15287  
    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();
Note: See TracChangeset for help on using the changeset viewer.