Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15215


Ignore:
Timestamp:
07/12/17 16:50:40 (7 years ago)
Author:
jkarder
Message:

#2258: worked on execution of basic algorithm

File:
1 edited

Legend:

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

    r15065 r15215  
    2222using System;
    2323using System.Threading;
    24 using System.Threading.Tasks;
    2524using HeuristicLab.Common;
    2625using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    6160    }
    6261
    63     public override void Start() {
    64       base.Start();
    65       CancellationTokenSource = new CancellationTokenSource();
     62    public override void Start(CancellationToken cancellationToken) {
     63      base.Start(cancellationToken);
     64      CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
    6665
    6766      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) {
    73           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);
    78           }
    79         }
    80         CancellationTokenSource.Dispose();
    81         CancellationTokenSource = null;
    82         OnStopped();
    83       });
     67      try {
     68        Run((object)cancellationTokenSource.Token);
     69      } catch (OperationCanceledException) {
     70      } catch (AggregateException ae) {
     71        if (ae.InnerExceptions.Count == 1) OnExceptionOccurred(ae.InnerExceptions[0]);
     72        else OnExceptionOccurred(ae);
     73      } catch (Exception e) {
     74        OnExceptionOccurred(e);
     75      }
     76
     77      CancellationTokenSource.Dispose();
     78      CancellationTokenSource = null;
     79      OnStopped();
    8480    }
    8581
Note: See TracChangeset for help on using the changeset viewer.