Ignore:
Timestamp:
11/23/15 21:18:26 (4 years ago)
Author:
jkarder
Message:

#2258: added StartAsync to IExecutable

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Async/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs

    r13000 r13349  
    2525using System.Linq;
    2626using System.Threading;
     27using System.Threading.Tasks;
    2728using HeuristicLab.Collections;
    2829using HeuristicLab.Common;
     
    3839  [StorableClass]
    3940  public sealed class Experiment : NamedItem, IOptimizer, IStorableContent {
     41    private readonly ManualResetEvent signaler = new ManualResetEvent(true);
     42
    4043    public string Filename { get; set; }
    4144
     
    186189    }
    187190    public void Start() {
    188       if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
    189         throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
    190       if (Optimizers.Count == 0) return;
    191 
    192       experimentStarted = true;
    193       experimentStopped = false;
    194       IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
    195       if (optimizer != null) {
    196         // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    197         try { optimizer.Start(); }
    198         catch (InvalidOperationException) { }
    199       }
     191      StartAsync().Wait();
     192    }
     193    public async Task StartAsync() {
     194      await StartAsync(new CancellationToken());
     195    }
     196    public async Task StartAsync(CancellationToken cancellationToken) {
     197      signaler.Reset();
     198      await Task.Run(async () => {
     199        if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
     200          throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
     201        if (Optimizers.Count == 0) return;
     202
     203        experimentStarted = true;
     204        experimentStopped = false;
     205        IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
     206        if (optimizer != null) {
     207          // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     208          try { await optimizer.StartAsync(cancellationToken); }
     209          catch (InvalidOperationException) { }
     210        }
     211        signaler.WaitOne();
     212      }, cancellationToken);
    200213    }
    201214    public void Pause() {
     
    261274    private void OnPaused() {
    262275      ExecutionState = ExecutionState.Paused;
     276      signaler.Set();
    263277      EventHandler handler = Paused;
    264278      if (handler != null) handler(this, EventArgs.Empty);
     
    267281    private void OnStopped() {
    268282      ExecutionState = ExecutionState.Stopped;
     283      signaler.Set();
    269284      EventHandler handler = Stopped;
    270285      if (handler != null) handler(this, EventArgs.Empty);
     
    380395        } else {
    381396          if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
    382             Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start();
     397            Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).StartAsync();
    383398          } else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped();
    384399          else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused();
Note: See TracChangeset for help on using the changeset viewer.