Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15752


Ignore:
Timestamp:
02/12/18 09:52:29 (6 years ago)
Author:
fholzing
Message:

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

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

    r15728 r15752  
    5555      this.dataPartitionComboBox = new System.Windows.Forms.ComboBox();
    5656      this.variableImactsArrayView = new HeuristicLab.Data.Views.StringConvertibleArrayView();
    57       this.backgroundWorker = new System.ComponentModel.BackgroundWorker();
    5857      this.SuspendLayout();
    5958      //
     
    170169      this.variableImactsArrayView.Size = new System.Drawing.Size(706, 278);
    171170      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);
    177171      //
    178172      // RegressionSolutionVariableImpactsView
     
    192186      this.Name = "RegressionSolutionVariableImpactsView";
    193187      this.Size = new System.Drawing.Size(712, 365);
     188      this.VisibleChanged += new System.EventHandler(this.RegressionSolutionVariableImpactsView_VisibleChanged);
    194189      this.ResumeLayout(false);
    195190      this.PerformLayout();
     
    209204    private System.Windows.Forms.ComboBox sortByComboBox;
    210205    private System.Windows.Forms.CheckBox ascendingCheckBox;
    211     private System.ComponentModel.BackgroundWorker backgroundWorker;
    212206  }
    213207}
  • trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs

    r15728 r15752  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Threading;
     26using System.Threading.Tasks;
    2527using HeuristicLab.Common;
    2628using HeuristicLab.Data;
     
    3234  public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
    3335    #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 
    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 RegressionSolutionVariableImpactsView_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 = 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     }
    168144    #endregion
    169145
    170146    #region Helper Methods   
    171     private void StartBackgroundWorker() {
     147    private void UpdateVariableImpact() {
    172148      //Check if the selection is valid
    173149      if (Content == null) { return; }
     
    181157      var factorReplMethod = (RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum)factorVarReplComboBox.Items[factorVarReplComboBox.SelectedIndex];
    182158      var dataPartition = (RegressionSolutionVariableImpactsCalculator.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 = RegressionSolutionVariableImpactsCalculator.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();
Note: See TracChangeset for help on using the changeset viewer.