Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/21/18 12:28:30 (7 years ago)
Author:
fholzing
Message:

#2871: Rebuild from BackgroundWorker to async await

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs

    r15796 r15797  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.ComponentModel;
    2524using System.Linq;
     25using System.Threading;
     26using System.Threading.Tasks;
    2627using HeuristicLab.Common;
    2728using HeuristicLab.Data;
     
    3233  [Content(typeof(IRegressionSolution))]
    3334  public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
    34     private class BackgroundworkerArguments {
    35       internal MainForm.WindowsForms.MainForm mainForm;
    36       internal RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum replMethod;
    37       internal RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum factorReplMethod;
    38       internal RegressionSolutionVariableImpactsCalculator.DataPartitionEnum dataPartition;
    39     }
     35    private CancellationTokenSource cancellationToken = new CancellationTokenSource();
    4036    private enum SortingCriteria {
    4137      ImpactValue,
     
    4541    private IProgress progress;
    4642    private Dictionary<string, double> rawVariableImpacts = new Dictionary<string, double>();
    47     private BackgroundWorker worker = new BackgroundWorker();
    4843
    4944    public new IRegressionSolution Content {
     
    6661      this.replacementComboBox.SelectedIndex = 0;
    6762      this.factorVarReplComboBox.SelectedIndex = 0;
    68 
    69       //Worker magic
    70       worker.WorkerSupportsCancellation = true;
    71       worker.WorkerReportsProgress = true;
    72       worker.DoWork += Worker_DoWork;
    73       worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
    74       worker.ProgressChanged += Worker_ProgressChanged;
    75     }
    76 
    77 
    78     private void Worker_DoWork(object sender, DoWorkEventArgs e) {
    79       var args = e.Argument as BackgroundworkerArguments;
    80 
    81       //Remember the original ordering of the variables
    82       var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, args.dataPartition, args.replMethod, args.factorReplMethod,
    83         (i) => {
    84           var worker = (sender as BackgroundWorker);
    85           worker.ReportProgress(0, i);
    86           return worker.CancellationPending;
    87         });
    88 
    89       if ((sender as BackgroundWorker).CancellationPending) { return; }
    90       var problemData = Content.ProblemData;
    91       var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
    92       var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList();
    93 
    94       rawVariableImpacts.Clear();
    95       originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2));
    96     }
    97     private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
    98       progress.ProgressValue = (double)e.UserState;
    99     }
    100     private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
    101       if (e.Error != null) { throw e.Error; }
    102       if (e.Error == null && !e.Cancelled) { UpdateDataOrdering(); }
    103       ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).RemoveOperationProgressFromView(this);
    10463    }
    10564
     
    13493
    13594    private void RegressionSolutionVariableImpactsView_VisibleChanged(object sender, EventArgs e) {
    136       if (!worker.IsBusy) { return; }
    137       worker.CancelAsync();
     95      if (!cancellationToken.IsCancellationRequested) {
     96        cancellationToken.Cancel();
     97      }
    13898    }
    13999
     
    173133
    174134
    175     private void UpdateVariableImpact() {
     135    private async void UpdateVariableImpact() {
    176136      //Check if the selection is valid
    177137      if (Content == null) { return; }
     
    190150      progress.ProgressValue = 0;
    191151
    192       if (!worker.IsBusy) {
    193         worker.RunWorkerAsync(new BackgroundworkerArguments() {
    194           mainForm = mainForm,
    195           dataPartition = dataPartition,
    196           factorReplMethod = factorReplMethod,
    197           replMethod = replMethod
    198         });
     152      cancellationToken = new CancellationTokenSource();
     153      //Remember the original ordering of the variables
     154      try {
     155        var impacts = await Task.Run(() => RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod,
     156          (i) => {
     157            progress.ProgressValue = i;
     158            return cancellationToken.Token.IsCancellationRequested;
     159          }), cancellationToken.Token);
     160
     161        if (cancellationToken.Token.IsCancellationRequested) { return; }
     162        var problemData = Content.ProblemData;
     163        var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
     164        var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList();
     165
     166        rawVariableImpacts.Clear();
     167        originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2));
     168        UpdateDataOrdering();
     169      } finally {
     170        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).RemoveOperationProgressFromView(this);
    199171      }
    200172    }
Note: See TracChangeset for help on using the changeset viewer.