Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5661


Ignore:
Timestamp:
03/10/11 13:15:37 (12 years ago)
Author:
gkronber
Message:

#1418 changed FixedDataAnalysisAlgorithm to start asynchronously (taken from Engine)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/FixedDataAnalysisAlgorithm.cs

    r5649 r5661  
    3232using System.Collections.Generic;
    3333using HeuristicLab.Problems.DataAnalysis.Symbolic;
     34using System.Threading.Tasks;
     35using System.Threading;
    3436
    3537namespace HeuristicLab.Algorithms.DataAnalysis {
     
    5557    }
    5658    #endregion
     59   
     60    private DateTime lastUpdateTime;
    5761
    5862    [StorableConstructor]
     
    7579    public override void Start() {
    7680      base.Start();
     81      var cancellationTokenSource = new CancellationTokenSource();
     82
    7783      OnStarted();
     84      Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
     85      task.ContinueWith(t => {
     86        try {
     87          t.Wait();
     88        }
     89        catch (AggregateException ex) {
     90          try {
     91            ex.Flatten().Handle(x => x is OperationCanceledException);
     92          }
     93          catch (AggregateException remaining) {
     94            if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
     95            else OnExceptionOccurred(remaining);
     96          }
     97        }
     98        cancellationTokenSource.Dispose();
     99        cancellationTokenSource = null;
     100        OnStopped();
     101      });
     102    }
     103    private void Run(object state) {
     104      CancellationToken cancellationToken = (CancellationToken)state;
     105      OnStarted();
     106      lastUpdateTime = DateTime.Now;
     107      System.Timers.Timer timer = new System.Timers.Timer(250);
     108      timer.AutoReset = true;
     109      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
     110      timer.Start();
    78111      try {
    79112        Run();
    80113      }
    81       catch (Exception e) {
    82         OnExceptionOccurred(e);
     114      finally {
     115        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
     116        timer.Stop();
     117        ExecutionTime += DateTime.Now - lastUpdateTime;
    83118      }
    84       finally {
    85         OnStopped();
    86       }
     119
     120      cancellationToken.ThrowIfCancellationRequested();
    87121    }
    88 
     122    protected abstract void Run();
    89123    #region Events
    90124    protected override void OnProblemChanged() {
     
    92126      base.OnProblemChanged();
    93127    }
    94 
     128    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
     129      System.Timers.Timer timer = (System.Timers.Timer)sender;
     130      timer.Enabled = false;
     131      DateTime now = DateTime.Now;
     132      ExecutionTime += now - lastUpdateTime;
     133      lastUpdateTime = now;
     134      timer.Enabled = true;
     135    }
    95136    #endregion
    96137
    97     protected abstract void Run();
    98138  }
    99139}
Note: See TracChangeset for help on using the changeset viewer.