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/TimeLimitRun.cs

    r13321 r13349  
    2525using System.Drawing;
    2626using System.Linq;
     27using System.Threading;
    2728using System.Threading.Tasks;
    2829using HeuristicLab.Collections;
     
    4041  [StorableClass]
    4142  public sealed class TimeLimitRun : NamedItem, IOptimizer, IStorableContent, INotifyPropertyChanged {
     43    private readonly ManualResetEvent signaler = new ManualResetEvent(true);
     44
    4245    public string Filename { get; set; }
    4346
     
    238241    }
    239242    public void Start() {
    240       Algorithm.Start();
     243      StartAsync().Wait();
     244    }
     245    public async Task StartAsync() {
     246      await StartAsync(new CancellationToken());
     247    }
     248    public async Task StartAsync(CancellationToken cancellationToken) {
     249      signaler.Reset();
     250      await Task.Run(async () => {
     251        await Algorithm.StartAsync(cancellationToken);
     252        signaler.WaitOne();
     253      }, cancellationToken);
    241254    }
    242255    public void Pause() {
     
    282295    public event EventHandler Paused;
    283296    private void OnPaused() {
     297      if (!pausedForSnapshot && !pausedForTermination) signaler.Set();
    284298      var handler = Paused;
    285299      if (handler != null) handler(this, EventArgs.Empty);
     
    287301    public event EventHandler Stopped;
    288302    private void OnStopped() {
     303      signaler.Set();
    289304      var handler = Stopped;
    290305      if (handler != null) handler(this, EventArgs.Empty);
     
    336351    private void Algorithm_Paused(object sender, EventArgs e) {
    337352      var action = pausedForTermination ? ExecutionState.Stopped : (pausedForSnapshot ? ExecutionState.Started : ExecutionState.Paused);
    338       if (pausedForSnapshot || pausedForTermination) {
    339         pausedForSnapshot = pausedForTermination = false;
     353      bool pausedByLimit = pausedForSnapshot || pausedForTermination;
     354      if (pausedByLimit) {
    340355        MakeSnapshot();
    341356        FindNextSnapshotTimeIndex(ExecutionTime);
    342357      }
    343358      OnPaused();
    344       if (action == ExecutionState.Started) Algorithm.Start();
     359      if (pausedByLimit) pausedForSnapshot = pausedForTermination = false;
     360      if (action == ExecutionState.Started) Algorithm.StartAsync();
    345361      else if (action == ExecutionState.Stopped) Algorithm.Stop();
    346362    }
Note: See TracChangeset for help on using the changeset viewer.