// On button "Start" // On button "Start" protected void startButton_Click(object sender, System.EventArgs e) { //EnableControls(false); //solutionBox.Text = string.Empty; AccordGenetic.Wrap.ApproximationWrap wrap = new AccordGenetic.Wrap.ApproximationWrap( data: dataToShow, functionsSet: FunctionsSetBox.Value.HasValue ? FunctionsSetBox.Value.Value : 0, populationSize: 40 /*PopulationSize.Value.Value*/, geneticMethod: 1,//GeneticMethod.Value.Value, selectionMethod: 0 /*SelectionMethod.Value.Value*/, minRange: (float)dataToShow[0, 0] /* (float)MinRange.Value.Value*/, lengthRange: (float)dataToShow[dataToShow.GetLength(0) - 1, 0] /*chart.RangeX.Length*/); SearchSolution(wrap); }
// Worker thread void SearchSolution(AccordGenetic.Wrap.ApproximationWrap wrap) { iterations = int.TryParse(iterationsBox.Text, out int result2) ? Math.Max(1, result2) : 100; var progressHandler = new Progress <KeyValuePair <int, AccordGenetic.Wrap.Result> >(kvp => ProgressUpdate(kvp, wrap)); cts = new CancellationTokenSource(); Task tsk = Task.Run(() => wrap.RunMultipleEpochs(iterations, cts.Token, progressHandler)); tsk.ContinueWith( t => { // faulted with exception if (t.IsFaulted) { Exception ex = t.Exception; while (ex is AggregateException && ex.InnerException != null) { ex = ex.InnerException; } MessageBox.Show("Error: " + ex.Message); } else if (t.IsCanceled) { //MessageBox.Show("Cancelled"); } else { if (!cts.IsCancellationRequested) { /* final update*/ } } // completed successfully/ check if closed button has been clicked if (!IsClosed) { EnableControls(true); } }); }