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:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources

  • trunk/sources/HeuristicLab.Optimization

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

    r14433 r15287  
    2424using System.Drawing;
    2525using System.Linq;
     26using System.Threading;
     27using System.Threading.Tasks;
    2628using HeuristicLab.Collections;
    2729using HeuristicLab.Common;
     
    203205    }
    204206    public virtual void Start() {
     207      Start(CancellationToken.None);
     208    }
     209    public virtual void Start(CancellationToken cancellationToken) {
    205210      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
    206211        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
     212    }
     213    public virtual async Task StartAsync() { await StartAsync(CancellationToken.None); }
     214    public virtual async Task StartAsync(CancellationToken cancellationToken) {
     215      await AsyncHelper.DoAsync(Start, cancellationToken);
    207216    }
    208217    public virtual void Pause() {
  • 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
  • trunk/sources/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs

    r14185 r15287  
    168168      }
    169169    }
    170     public override void Start() {
    171       base.Start();
    172       if (engine != null) engine.Start();
     170    public override void Start(System.Threading.CancellationToken cancellationToken) {
     171      base.Start(cancellationToken);
     172      if (engine != null) engine.Start(cancellationToken);
    173173    }
    174174    public override void Pause() {
Note: See TracChangeset for help on using the changeset viewer.