/// <summary> /// Handles the DoWork event of the backgroundWorker control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs" /> instance containing the event data.</param> private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { long begin, elapsed; try { while (true) { begin = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; ga.Run(); backgroundWorker.ReportProgress(0); if (ga.SolutionFound || ga.Converged || ga.Terminated || stopRequested) { break; } elapsed = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - begin; Thread.Sleep((int)Math.Max(0, 10 - elapsed)); } } catch (Exception ex) { backgroundWorker.ReportProgress(0, ex); } }
public void Run() { ConsoleEx.WriteLineGreen("\nGenetic algorithm started...\n"); try { _geneticAlgorithm.StateChanged += OnGeneticAlgorithmStateChanged; _geneticAlgorithm.Terminated += OnGeneticAlgorithmTerminated; _geneticAlgorithm.Run(); } catch (Exception ex) { ConsoleEx.WriteLineRed(ex.StackTrace); } }