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.Optimization

  • trunk/sources/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs

    r14523 r15287  
    2121
    2222using System;
     23using System.Linq;
    2324using System.Threading;
    24 using System.Threading.Tasks;
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
     
    7171    }
    7272
    73     public override void Start() {
    74       base.Start();
    75       CancellationTokenSource = new CancellationTokenSource();
     73    public override void Start(CancellationToken cancellationToken) {
     74      base.Start(cancellationToken);
     75      CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
    7676      pausePending = false;
    7777      OnStarted();
    7878
    79       Task task = Task.Factory.StartNew(Run, CancellationTokenSource.Token, CancellationTokenSource.Token);
    80       task.ContinueWith(t => {
    81         try {
    82           t.Wait();
    83         }
    84         catch (AggregateException ex) {
    85           try {
    86             ex.Flatten().Handle(x => x is OperationCanceledException);
    87           }
    88           catch (AggregateException remaining) {
    89             if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
    90             else OnExceptionOccurred(remaining);
    91           }
    92         }
    93         CancellationTokenSource.Dispose();
    94         CancellationTokenSource = null;
    95         if (pausePending) OnPaused();
    96         else OnStopped();
    97       });
     79      try {
     80        Run((object)cancellationTokenSource.Token);
     81      } catch (OperationCanceledException) {
     82      } catch (AggregateException ae) {
     83        OnExceptionOccurred(ae.InnerExceptions.SingleOrDefault() ?? ae);
     84      } catch (Exception e) {
     85        OnExceptionOccurred(e);
     86      }
     87
     88      CancellationTokenSource.Dispose();
     89      CancellationTokenSource = null;
     90      if (pausePending) OnPaused();
     91      else OnStopped();
    9892    }
    9993
Note: See TracChangeset for help on using the changeset viewer.