Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/07/18 13:29:06 (7 years ago)
Author:
fholzing
Message:

#2884: Fixed crossthreading-bug

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

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionVariableImpactsView.Designer.cs

    r15667 r15729  
    5454      this.sortByLabel = new System.Windows.Forms.Label();
    5555      this.sortByComboBox = new System.Windows.Forms.ComboBox();
     56      this.backgroundWorker = new System.ComponentModel.BackgroundWorker();
    5657      this.SuspendLayout();
    5758      //
     
    168169      this.sortByComboBox.TabIndex = 9;
    169170      this.sortByComboBox.SelectedIndexChanged += new System.EventHandler(this.sortByComboBox_SelectedIndexChanged);
     171      //
     172      // backgroundWorker
     173      //
     174      this.backgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker_DoWork);
     175      this.backgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker_RunWorkerCompleted);
    170176      //
    171177      // ClassificationSolutionVariableImpactsView
     
    202208    private System.Windows.Forms.Label sortByLabel;
    203209    private System.Windows.Forms.ComboBox sortByComboBox;
     210    private System.ComponentModel.BackgroundWorker backgroundWorker;
    204211  }
    205212}
  • trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionVariableImpactsView.cs

    r15674 r15729  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using System.Threading.Tasks;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Data;
     
    3231  [Content(typeof(IClassificationSolution))]
    3332  public partial class ClassificationSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
     33    #region Nested Types
     34    private class BackgroundWorkerArguments {
     35      internal MainForm.WindowsForms.MainForm mainForm;
     36      internal ClassificationSolutionVariableImpactsCalculator.ReplacementMethodEnum replMethod;
     37      internal ClassificationSolutionVariableImpactsCalculator.FactorReplacementMethodEnum factorReplMethod;
     38      internal ClassificationSolutionVariableImpactsCalculator.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 IClassificationSolution Content {
    4354      get { return (IClassificationSolution)base.Content; }
     
    4657      }
    4758    }
    48 
     59    #endregion
     60
     61    #region Ctor
    4962    public ClassificationSolutionVariableImpactsView()
    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    }
     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 = ClassificationSolutionVariableImpactsCalculator.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    }
    125168    #endregion
    126169
    127170    #region Helper Methods
    128     private void UpdateVariableImpacts() {
     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 = (ClassificationSolutionVariableImpactsCalculator.ReplacementMethodEnum)replacementComboBox.Items[replacementComboBox.SelectedIndex];
    138181      var factorReplMethod = (ClassificationSolutionVariableImpactsCalculator.FactorReplacementMethodEnum)factorVarReplComboBox.Items[factorVarReplComboBox.SelectedIndex];
    139182      var dataPartition = (ClassificationSolutionVariableImpactsCalculator.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 = ClassificationSolutionVariableImpactsCalculator.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();
    196     }
    197     #endregion   
     231
     232      //Could be, if the View was closed during the BackgroundWorker-run
     233      if (!variableImactsArrayView.IsDisposed) {
     234        variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
     235      }
     236    }
     237    #endregion
    198238  }
    199239}
Note: See TracChangeset for help on using the changeset viewer.