Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/31/10 04:29:19 (14 years ago)
Author:
swagner
Message:

Worked on cancellation and refactored code (#1333)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ParallelEngine/HeuristicLab.Core/3.3/Engine.cs

    r4722 r5185  
    2323using System.Collections.Generic;
    2424using System.Threading;
     25using System.Threading.Tasks;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    4243    }
    4344
    44     private bool pausePending, stopPending;
     45    #region Variables for communication between threads
     46    private CancellationTokenSource cancellationTokenSource;
     47    private bool stopRequested;
    4548    private DateTime lastUpdateTime;
    46     private System.Timers.Timer timer;
     49    #endregion
    4750
    4851    [StorableConstructor]
    49     protected Engine(bool deserializing)
    50       : base(deserializing) {
    51       pausePending = stopPending = false;
    52       timer = new System.Timers.Timer(100);
    53       timer.AutoReset = true;
    54       timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    55     }
     52    protected Engine(bool deserializing) : base(deserializing) { }
    5653    protected Engine(Engine original, Cloner cloner)
    5754      : base(original, cloner) {
     
    6259      for (int i = contexts.Length - 1; i >= 0; i--)
    6360        executionStack.Push(cloner.Clone(contexts[i]));
    64       pausePending = original.pausePending;
    65       stopPending = original.stopPending;
    66       timer = new System.Timers.Timer(100);
    67       timer.AutoReset = true;
    68       timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    6961    }
    7062    protected Engine()
     
    7264      log = new Log();
    7365      executionStack = new Stack<IOperation>();
    74       pausePending = stopPending = false;
    75       timer = new System.Timers.Timer(100);
    76       timer.AutoReset = true;
    77       timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    7866    }
    7967
     
    9785    public override void Start() {
    9886      base.Start();
    99       ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null);
     87      cancellationTokenSource = new CancellationTokenSource();
     88      stopRequested = false;
     89      Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
     90      task.ContinueWith(t => {
     91        try {
     92          t.Wait();
     93        }
     94        catch (AggregateException ex) {
     95          ex.Flatten().Handle(x => {
     96            if (!(x is OperationCanceledException)) OnExceptionOccurred(x);
     97            return true;
     98          });
     99        }
     100        cancellationTokenSource.Dispose();
     101        cancellationTokenSource = null;
     102        if (stopRequested) {
     103          executionStack.Clear();
     104          OnStopped();
     105        } else {
     106          if (executionStack.Count == 0) OnStopped();
     107          else OnPaused();
     108        }
     109      });
    100110    }
    101111    protected override void OnStarted() {
     
    106116    public override void Pause() {
    107117      base.Pause();
    108       pausePending = true;
     118      cancellationTokenSource.Cancel();
    109119    }
    110120    protected override void OnPaused() {
     
    115125    public override void Stop() {
    116126      base.Stop();
    117       stopPending = true;
    118127      if (ExecutionState == ExecutionState.Paused) OnStopped();
     128      else {
     129        stopRequested = true;
     130        cancellationTokenSource.Cancel();
     131      }
    119132    }
    120133    protected override void OnStopped() {
     
    129142
    130143    private void Run(object state) {
     144      CancellationToken cancellationToken = (CancellationToken)state;
     145
    131146      OnStarted();
    132       pausePending = stopPending = false;
     147      lastUpdateTime = DateTime.Now;
     148      System.Timers.Timer timer = new System.Timers.Timer(100);
     149      timer.AutoReset = true;
     150      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
     151      timer.Start();
     152      try {
     153        Run(cancellationToken);
     154      }
     155      finally {
     156        timer.Stop();
     157        timer.Dispose();
     158        ExecutionTime += DateTime.Now - lastUpdateTime;
     159      }
    133160
    134       lastUpdateTime = DateTime.Now;
    135       timer.Start();
    136       while (!pausePending && !stopPending && (executionStack.Count > 0)) {
    137         ProcessNextOperation();
    138       }
    139       timer.Stop();
    140       ExecutionTime += DateTime.Now - lastUpdateTime;
    141 
    142       if (pausePending) OnPaused();
    143       else OnStopped();
     161      cancellationToken.ThrowIfCancellationRequested();
    144162    }
    145 
    146     protected abstract void ProcessNextOperation();
     163    protected abstract void Run(CancellationToken cancellationToken);
    147164
    148165    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
Note: See TracChangeset for help on using the changeset viewer.