Changeset 15753


Ignore:
Timestamp:
02/12/18 09:58:33 (14 months ago)
Author:
fholzing
Message:

#2884: Changed back to Task, abort Thread if any other View is shown

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

    r15729 r15753  
    170170      this.sortByComboBox.SelectedIndexChanged += new System.EventHandler(this.sortByComboBox_SelectedIndexChanged);
    171171      //
    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);
    176       //
    177172      // ClassificationSolutionVariableImpactsView
    178173      //
     
    191186      this.Name = "ClassificationSolutionVariableImpactsView";
    192187      this.Size = new System.Drawing.Size(668, 365);
     188      this.VisibleChanged += new System.EventHandler(this.ClassificationSolutionVariableImpactsView_VisibleChanged);
    193189      this.ResumeLayout(false);
    194190      this.PerformLayout();
  • trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionVariableImpactsView.cs

    r15729 r15753  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Threading;
     26using System.Threading.Tasks;
    2527using HeuristicLab.Common;
    2628using HeuristicLab.Data;
     
    3234  public partial class ClassificationSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
    3335    #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 
    4136    private enum SortingCriteria {
    4237      ImpactValue,
     
    4843    #region Fields
    4944    private Dictionary<string, double> rawVariableImpacts = new Dictionary<string, double>();
     45    private Thread thread;
    5046    #endregion
    5147
     
    10197        variableImactsArrayView.Content = null;
    10298      } else {
    103         StartBackgroundWorker();
    104       }
     99        UpdateVariableImpact();
     100      }
     101    }
     102
     103    private void ClassificationSolutionVariableImpactsView_VisibleChanged(object sender, EventArgs e) {
     104      if (thread == null) { return; }
     105
     106      if (thread.IsAlive) { thread.Abort(); }
     107      thread = null;
    105108    }
    106109
    107110
    108111    private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    109       StartBackgroundWorker();
     112      UpdateVariableImpact();
    110113    }
    111114
    112115    private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    113       StartBackgroundWorker();
     116      UpdateVariableImpact();
    114117    }
    115118
     
    139142    }
    140143
    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     }
    168     #endregion
    169 
    170     #region Helper Methods
    171     private void StartBackgroundWorker() {
     144    #endregion
     145
     146    #region Helper Methods   
     147    private void UpdateVariableImpact() {
    172148      //Check if the selection is valid
    173149      if (Content == null) { return; }
     
    181157      var factorReplMethod = (ClassificationSolutionVariableImpactsCalculator.FactorReplacementMethodEnum)factorVarReplComboBox.Items[factorVarReplComboBox.SelectedIndex];
    182158      var dataPartition = (ClassificationSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem;
    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); }
     159
     160      variableImactsArrayView.Caption = Content.Name + " Variable Impacts";
     161
     162      mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name);
     163
     164      Task.Factory.StartNew(() => {
     165        thread = Thread.CurrentThread;
     166        //Remember the original ordering of the variables
     167        var impacts = ClassificationSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod);
     168        var problemData = Content.ProblemData;
     169        var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
     170        var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList();
     171
     172        rawVariableImpacts.Clear();
     173        originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2));
     174      }).ContinueWith((o) => {
     175        UpdateDataOrdering();
     176        mainForm.RemoveOperationProgressFromView(this);
     177        thread = null;
     178      }, TaskScheduler.FromCurrentSynchronizationContext());
    193179    }
    194180
     
    230216      };
    231217
    232       //Could be, if the View was closed during the BackgroundWorker-run
     218      //Could be, if the View was closed
    233219      if (!variableImactsArrayView.IsDisposed) {
    234220        variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
    235221      }
    236222    }
    237     #endregion
     223    #endregion  
    238224  }
    239225}
Note: See TracChangeset for help on using the changeset viewer.