Changeset 5661
- Timestamp:
- 03/10/11 13:15:37 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/FixedDataAnalysisAlgorithm.cs
r5649 r5661 32 32 using System.Collections.Generic; 33 33 using HeuristicLab.Problems.DataAnalysis.Symbolic; 34 using System.Threading.Tasks; 35 using System.Threading; 34 36 35 37 namespace HeuristicLab.Algorithms.DataAnalysis { … … 55 57 } 56 58 #endregion 59 60 private DateTime lastUpdateTime; 57 61 58 62 [StorableConstructor] … … 75 79 public override void Start() { 76 80 base.Start(); 81 var cancellationTokenSource = new CancellationTokenSource(); 82 77 83 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(); 78 111 try { 79 112 Run(); 80 113 } 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; 83 118 } 84 finally { 85 OnStopped(); 86 } 119 120 cancellationToken.ThrowIfCancellationRequested(); 87 121 } 88 122 protected abstract void Run(); 89 123 #region Events 90 124 protected override void OnProblemChanged() { … … 92 126 base.OnProblemChanged(); 93 127 } 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 } 95 136 #endregion 96 137 97 protected abstract void Run();98 138 } 99 139 }
Note: See TracChangeset
for help on using the changeset viewer.