Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15727


Ignore:
Timestamp:
02/07/18 12:47:26 (6 years ago)
Author:
fholzing
Message:

#2871: Fixed crossthreading-bug

Location:
trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression
Files:
2 edited

Legend:

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

    r15673 r15727  
    5555      this.dataPartitionComboBox = new System.Windows.Forms.ComboBox();
    5656      this.variableImactsArrayView = new HeuristicLab.Data.Views.StringConvertibleArrayView();
     57      this.backgroundWorker = new System.ComponentModel.BackgroundWorker();
    5758      this.SuspendLayout();
    5859      //
     
    169170      this.variableImactsArrayView.Size = new System.Drawing.Size(706, 278);
    170171      this.variableImactsArrayView.TabIndex = 2;
     172      //
     173      // backgroundWorker
     174      //
     175      this.backgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.BackgroundWorker_DoWork);
     176      this.backgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.BackgroundWorker_RunWorkerCompleted);
    171177      //
    172178      // RegressionSolutionVariableImpactsView
     
    203209    private System.Windows.Forms.ComboBox sortByComboBox;
    204210    private System.Windows.Forms.CheckBox ascendingCheckBox;
     211    private System.ComponentModel.BackgroundWorker backgroundWorker;
    205212  }
    206213}
  • trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs

    r15673 r15727  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using System.Threading.Tasks;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Data;
     
    3231  [Content(typeof(IRegressionSolution))]
    3332  public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
     33    #region Nested Types
     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    }
     40
    3441    private enum SortingCriteria {
    3542      ImpactValue,
     
    3744      VariableName
    3845    }
    39 
     46    #endregion
     47
     48    #region Fields
    4049    private Dictionary<string, double> rawVariableImpacts = new Dictionary<string, double>();
    41 
     50    #endregion
     51
     52    #region Getter/Setter
    4253    public new IRegressionSolution Content {
    4354      get { return (IRegressionSolution)base.Content; }
     
    4657      }
    4758    }
    48 
     59    #endregion
     60
     61    #region Ctor
    4962    public RegressionSolutionVariableImpactsView()
    5063      : base() {
     
    5568      this.sortByComboBox.SelectedItem = SortingCriteria.ImpactValue;
    5669
     70      //Set the default values
    5771      this.dataPartitionComboBox.SelectedIndex = 0;
    5872      this.replacementComboBox.SelectedIndex = 0;
    5973      this.factorVarReplComboBox.SelectedIndex = 0;
    6074    }
    61 
    62     #region events
     75    #endregion
     76
     77    #region Events
    6378    protected override void RegisterContentEvents() {
    6479      base.RegisterContentEvents();
     
    86101        variableImactsArrayView.Content = null;
    87102      } else {
    88         UpdateVariableImpacts();
     103        StartBackgroundWorker();
    89104      }
    90105    }
     
    92107
    93108    private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    94       UpdateVariableImpacts();
     109      StartBackgroundWorker();
    95110    }
    96111
    97112    private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    98       UpdateVariableImpacts();
     113      StartBackgroundWorker();
    99114    }
    100115
     
    123138      UpdateDataOrdering();
    124139    }
    125     #endregion
    126 
    127     #region Helper Methods         
    128     private void UpdateVariableImpacts() {
     140
     141
     142    private void BackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) {
     143      variableImactsArrayView.Caption = Content.Name + " Variable Impacts";
     144
     145      var argument = e.Argument as BackgroundWorkerArguments;
     146      if (!(argument is BackgroundWorkerArguments)) {
     147        throw new ArgumentException("Argument for Backgroundworker must be of type BackgroundworkerArguments");
     148      }
     149
     150      argument.mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name);
     151      try {
     152        //Remember the original ordering of the variables
     153        var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, argument.dataPartition, argument.replMethod, argument.factorReplMethod);
     154        var problemData = Content.ProblemData;
     155        var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
     156        var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList();
     157
     158        rawVariableImpacts.Clear();
     159        originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2));
     160      } finally {
     161        argument.mainForm.RemoveOperationProgressFromView(this);
     162      }
     163    }
     164
     165    private void BackgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) {
     166      UpdateDataOrdering();
     167    }
     168    #endregion
     169
     170    #region Helper Methods   
     171    private void StartBackgroundWorker() {
     172      //Check if the selection is valid
    129173      if (Content == null) { return; }
    130174      if (replacementComboBox.SelectedIndex < 0) { return; }
     
    132176      if (factorVarReplComboBox.SelectedIndex < 0) { return; }
    133177
    134       variableImactsArrayView.Caption = Content.Name + " Variable Impacts";
    135 
     178      //Prepare arguments
    136179      var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
    137180      var replMethod = (RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum)replacementComboBox.Items[replacementComboBox.SelectedIndex];
    138181      var factorReplMethod = (RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum)factorVarReplComboBox.Items[factorVarReplComboBox.SelectedIndex];
    139182      var dataPartition = (RegressionSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem;
    140 
    141       Task.Factory.StartNew(() => {
    142         try {
    143           mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name);
    144 
    145           //Remember the original ordering of the variables
    146           var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod);
    147           var problemData = Content.ProblemData;
    148           var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
    149           var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList();
    150           rawVariableImpacts.Clear();
    151           originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2));
    152           UpdateDataOrdering();
    153         } finally {
    154           mainForm.RemoveOperationProgressFromView(this);
    155         }
    156       });
     183      var args = new BackgroundWorkerArguments() {
     184        mainForm = mainForm,
     185        replMethod = replMethod,
     186        factorReplMethod = factorReplMethod,
     187        dataPartition = dataPartition
     188      };
     189
     190      //Let the backgroundWorker do his job (unless he's already running)
     191      //fholzing: Possible bug -> A ContentChanged won't update the data if the backgroundworker is already running
     192      if (!backgroundWorker.IsBusy) { backgroundWorker.RunWorkerAsync(args); }
    157193    }
    158194
     
    193229        ElementNames = orderedEntries.Select(i => i.Key)
    194230      };
    195       variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
     231
     232      //Could be, if the View was closed during the BackgroundWorker-run
     233      if (!variableImactsArrayView.IsDisposed) {
     234        variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
     235      }
    196236    }
    197237    #endregion
Note: See TracChangeset for help on using the changeset viewer.