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

#2258: added StartAsync to IExecutable

File:
1 edited

Legend:

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

    r12504 r13349  
    2323using System.Collections.Generic;
    2424using System.Drawing;
     25using System.Threading;
     26using System.Threading.Tasks;
    2527using HeuristicLab.Collections;
    2628using HeuristicLab.Common;
     
    3941  [StorableClass]
    4042  public sealed class BatchRun : NamedItem, IOptimizer, IStorableContent {
     43    private readonly ManualResetEvent signaler = new ManualResetEvent(true);
     44
    4145    public string Filename { get; set; }
    4246
     
    244248    }
    245249    public void Start() {
    246       if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
    247         throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
    248       if (Optimizer == null) return;
    249       batchRunAction = BatchRunAction.Start;
    250       if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare();
    251       // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    252       try { Optimizer.Start(); }
    253       catch (InvalidOperationException) { }
     250      StartAsync().Wait();
     251    }
     252    public async Task StartAsync() {
     253      await StartAsync(new CancellationToken());
     254    }
     255    public async Task StartAsync(CancellationToken cancellationToken) {
     256      signaler.Reset();
     257      await Task.Run(async () => {
     258        if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
     259          throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
     260        if (Optimizer == null) return;
     261        batchRunAction = BatchRunAction.Start;
     262        if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare();
     263        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     264        try { await Optimizer.StartAsync(cancellationToken); }
     265        catch (InvalidOperationException) { }
     266        signaler.WaitOne();
     267      }, cancellationToken);
    254268    }
    255269    public void Pause() {
     
    326340      batchRunAction = BatchRunAction.None;
    327341      ExecutionState = ExecutionState.Paused;
     342      signaler.Set();
    328343      EventHandler handler = Paused;
    329344      if (handler != null) handler(this, EventArgs.Empty);
     
    333348      batchRunAction = BatchRunAction.None;
    334349      ExecutionState = ExecutionState.Stopped;
     350      signaler.Set();
    335351      EventHandler handler = Stopped;
    336352      if (handler != null) handler(this, EventArgs.Empty);
     
    397413      else if (batchRunAction == BatchRunAction.Start) {
    398414        Optimizer.Prepare();
    399         Optimizer.Start();
     415        Optimizer.StartAsync();
    400416      } else if (executionState == ExecutionState.Started) {
    401417        // if the batch run hasn't been started but the inner optimizer was run then pause
Note: See TracChangeset for help on using the changeset viewer.