- Timestamp:
- 07/17/11 22:51:11 (13 years ago)
- Location:
- branches/QAPAlgorithms
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/QAPAlgorithms
- Property svn:ignore
-
old new 12 12 *.psess 13 13 *.vsp 14 *.docstates
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/QAPAlgorithms/HeuristicLab/3.3/Tests/AlgorithmExtensions.cs
r6205 r6569 22 22 using System; 23 23 using System.Threading; 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 using HeuristicLab.Optimization;26 26 27 27 namespace HeuristicLab_33.Tests { 28 28 public static class AlgorithmExtensions { 29 public static void StartSync(this I Algorithm algorithm, CancellationToken cancellationToken) {30 var executor = new AlgorithmExecutor( algorithm, cancellationToken);29 public static void StartSync(this IExecutable executable, CancellationToken cancellationToken) { 30 var executor = new AlgorithmExecutor(executable, cancellationToken); 31 31 executor.StartSync(); 32 32 } … … 37 37 /// </summary> 38 38 internal class AlgorithmExecutor { 39 private I Algorithm algorithm;39 private IExecutable executable; 40 40 private AutoResetEvent waitHandle = new AutoResetEvent(false); 41 41 private CancellationToken cancellationToken; 42 private Exception occuredException; 42 43 43 public AlgorithmExecutor(I Algorithm algorithm, CancellationToken cancellationToken) {44 this. algorithm = algorithm;44 public AlgorithmExecutor(IExecutable executable, CancellationToken cancellationToken) { 45 this.executable = executable; 45 46 this.cancellationToken = cancellationToken; 47 this.occuredException = null; 46 48 } 47 49 48 50 public void StartSync() { 49 algorithm.Stopped += new EventHandler(algorithm_Stopped); 50 algorithm.Paused += new EventHandler(algorithm_Paused); 51 executable.Stopped += new EventHandler(executable_Stopped); 52 executable.Paused += new EventHandler(executable_Paused); 53 executable.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(executable_ExceptionOccurred); 51 54 52 55 using (CancellationTokenRegistration registration = cancellationToken.Register(new Action(cancellationToken_Canceled))) { 53 algorithm.Start();54 waitHandle.WaitOne( );56 executable.Start(); 57 waitHandle.WaitOne(-1, false); 55 58 waitHandle.Dispose(); 56 59 } 57 60 58 algorithm.Stopped -= new EventHandler(algorithm_Stopped);59 algorithm.Paused -= new EventHandler(algorithm_Paused);60 if ( algorithm.ExecutionState == ExecutionState.Started) {61 algorithm.Pause();61 executable.Stopped -= new EventHandler(executable_Stopped); 62 executable.Paused -= new EventHandler(executable_Paused); 63 if (executable.ExecutionState == ExecutionState.Started) { 64 executable.Pause(); 62 65 } 63 66 cancellationToken.ThrowIfCancellationRequested(); 67 if (occuredException != null) throw occuredException; 64 68 } 65 69 66 private void algorithm_Paused(object sender, EventArgs e) {70 private void executable_Paused(object sender, EventArgs e) { 67 71 waitHandle.Set(); 68 72 } 69 73 70 private void algorithm_Stopped(object sender, EventArgs e) {74 private void executable_Stopped(object sender, EventArgs e) { 71 75 waitHandle.Set(); 76 } 77 78 private void executable_ExceptionOccurred(object sender, EventArgs<Exception> e) { 79 occuredException = e.Value; // after an exception occured the executable pauses 72 80 } 73 81
Note: See TracChangeset
for help on using the changeset viewer.