Changeset 17426


Ignore:
Timestamp:
02/06/20 16:54:29 (9 months ago)
Author:
pfleck
Message:

#2973

  • Certain events of the TargetControl are now forwarded to the ProgressView in order to correctly dispose and hide the ProgressView.
  • Simplified hiding the ProgressView within the Regression/ClassificationSolutionVariableImpactsView.
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs

    r17180 r17426  
    2525namespace HeuristicLab.MainForm.WindowsForms {
    2626  internal sealed partial class ProgressView : UserControl {
    27     private readonly Control control;
    28     public Control Control {
    29       get { return control; }
    30     }
    31 
    32     private readonly IProgress content;
    33     public IProgress Content {
    34       get { return content; }
    35     }
    36 
    37     public ProgressView(Control control, IProgress content)
     27    public Control TargetControl { get; }
     28    public IProgress Content { get; }
     29
     30    public ProgressView(Control targetControl, IProgress content)
    3831      : base() {
    39       if (control == null) throw new ArgumentNullException("control");
    40       if (control.Parent == null) throw new InvalidOperationException("A Progress can only be shown on controls that have a Parent-control. Therefore, Dialogs and Forms cannot have an associated ProgressView.");
    41       if (content == null) throw new ArgumentNullException("content");
     32      if (targetControl == null) throw new ArgumentNullException(nameof(targetControl));
     33      if (targetControl.Parent == null) throw new InvalidOperationException("A Progress can only be shown on controls that have a Parent-control. Therefore, Dialogs and Forms cannot have an associated ProgressView.");
     34      if (content == null) throw new ArgumentNullException(nameof(content));
    4235      InitializeComponent();
    4336
    44       this.control = control;
    45       this.content = content;
     37      this.TargetControl = targetControl;
     38      this.Content = content;
    4639
    4740      if (content.ProgressState != ProgressState.Finished)
     
    5245    protected override void Dispose(bool disposing) {
    5346      DeregisterContentEvents();
    54       HideProgress();
    55 
    56       if (disposing && (components != null)) {
     47
     48      if (!TargetControl.IsDisposed)
     49        HideProgress();
     50
     51      if (disposing && components != null) {
    5752        components.Dispose();
    5853      }
     
    10196
    10297    private void ShowProgress() {
    103       if (Control.InvokeRequired) {
    104         Control.Invoke((Action)ShowProgress);
     98      if (TargetControl.InvokeRequired) {
     99        TargetControl.Invoke((Action)ShowProgress);
    105100        return;
    106101      }
    107102      if (Parent != null) return;
    108103
    109       Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
    110       Top = (Control.ClientRectangle.Height / 2) - (Height / 2);
     104      Left = (TargetControl.ClientRectangle.Width / 2) - (Width / 2);
     105      Top = (TargetControl.ClientRectangle.Height / 2) - (Height / 2);
    111106      Anchor = AnchorStyles.None;
    112107
     
    115110      UpdateButtonsState();
    116111
    117       Control.SuspendRepaint();
    118       Control.Enabled = false;
    119       Parent = Control.Parent;
     112      TargetControl.SuspendRepaint();
     113      TargetControl.Enabled = false;
     114      RegisterTargetControlEvents();
     115      Parent = TargetControl.Parent;
    120116      BringToFront();
    121       Control.ResumeRepaint(true);
     117      TargetControl.ResumeRepaint(true);
    122118      Visible = true;
    123119    }
    124120
    125121    private void HideProgress() {
    126       if (Control.InvokeRequired) {
    127         Control.Invoke((Action)HideProgress);
     122      if (TargetControl.InvokeRequired) {
     123        TargetControl.Invoke((Action)HideProgress);
    128124        return;
    129125      }
     
    131127
    132128      Visible = false;
    133       Control.SuspendRepaint();
    134       Control.Enabled = true;
    135       Control.ResumeRepaint(true);
     129      TargetControl.SuspendRepaint();
     130      TargetControl.Enabled = true;
     131      DeregisterTargetControlEvents();
    136132      Parent = null;
     133      TargetControl.ResumeRepaint(TargetControl.Visible);
     134    }
     135
     136
     137    private void RegisterTargetControlEvents() {
     138      TargetControl.Disposed += TargetControl_Disposed;
     139      TargetControl.VisibleChanged += TargetControl_VisibleChanged;
     140      TargetControl.ParentChanged += TargetControl_ParentChanged;
     141    }
     142
     143    private void DeregisterTargetControlEvents() {
     144      TargetControl.Disposed -= TargetControl_Disposed;
     145      TargetControl.VisibleChanged -= TargetControl_VisibleChanged;
     146      TargetControl.ParentChanged -= TargetControl_ParentChanged;
     147    }
     148
     149    private void TargetControl_Disposed(object sender, EventArgs e) {
     150      Dispose();
     151    }
     152    private void TargetControl_VisibleChanged(object sender, EventArgs e) {
     153      Visible = TargetControl.Visible;
     154    }
     155    private void TargetControl_ParentChanged(object sender, EventArgs e) {
     156      Parent = TargetControl.Parent;
    137157    }
    138158
    139159    private void UpdateProgressState() {
    140       if (Control.InvokeRequired) {
    141         Control.Invoke((Action)UpdateProgressState);
     160      if (TargetControl.InvokeRequired) {
     161        TargetControl.Invoke((Action)UpdateProgressState);
    142162        return;
    143163      }
     
    150170
    151171    private void UpdateProgressMessage() {
    152       if (Control.InvokeRequired) {
    153         Control.Invoke((Action)UpdateProgressMessage);
    154         return;
    155       }
    156 
    157       messageLabel.Text = content.Message;
     172      if (TargetControl.InvokeRequired) {
     173        TargetControl.Invoke((Action)UpdateProgressMessage);
     174        return;
     175      }
     176
     177      messageLabel.Text = Content.Message;
    158178    }
    159179
     
    167187        case ProgressMode.Determinate:
    168188          progressBar.Style = ProgressBarStyle.Continuous;
    169           progressBar.Value = (int)Math.Round(progressBar.Minimum + content.ProgressValue * (progressBar.Maximum - progressBar.Minimum));
     189          progressBar.Value = (int)Math.Round(progressBar.Minimum + Content.ProgressValue * (progressBar.Maximum - progressBar.Minimum));
    170190          break;
    171191        case ProgressMode.Indeterminate:
     
    174194          break;
    175195        default:
    176           throw new NotImplementedException($"Invalid Progress Mode: {content.ProgressMode}");
     196          throw new NotImplementedException($"Invalid Progress Mode: {Content.ProgressMode}");
    177197      }
    178198    }
    179199
    180200    private void UpdateButtonsState() {
    181       if (Control.InvokeRequired) {
    182         Control.Invoke((Action)UpdateButtonsState);
     201      if (TargetControl.InvokeRequired) {
     202        TargetControl.Invoke((Action)UpdateButtonsState);
    183203        return;
    184204      }
    185205
    186206      stopButton.Visible = Content.CanBeStopped;
    187       stopButton.Enabled = Content.CanBeStopped && content.ProgressState == ProgressState.Started;
     207      stopButton.Enabled = Content.CanBeStopped && Content.ProgressState == ProgressState.Started;
    188208
    189209      cancelButton.Visible = Content.CanBeCanceled;
    190       cancelButton.Enabled = Content.CanBeCanceled && content.ProgressState == ProgressState.Started;
     210      cancelButton.Enabled = Content.CanBeCanceled && Content.ProgressState == ProgressState.Started;
    191211    }
    192212
  • trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionVariableImpactsView.cs

    r17276 r17426  
    2525using System.Threading;
    2626using System.Threading.Tasks;
     27using System.Windows.Forms;
    2728using HeuristicLab.Common;
    2829using HeuristicLab.Data;
     
    4041    private CancellationTokenSource cancellationToken = new CancellationTokenSource();
    4142    private List<Tuple<string, double>> rawVariableImpacts = new List<Tuple<string, double>>();
    42     private bool attachedToProgress = false;
    4343
    4444    public new IClassificationSolution Content {
     
    8787      }
    8888    }
    89     protected override void OnHidden(EventArgs e) {
    90       base.OnHidden(e);
     89    protected override void OnVisibleChanged(EventArgs e) {
     90      base.OnVisibleChanged(e);
     91      if (!this.Visible) {
     92        cancellationToken.Cancel();
     93      }
     94    }
     95
     96    protected override void OnClosed(FormClosedEventArgs e) {
     97      base.OnClosed(e);
    9198      cancellationToken.Cancel();
    92 
    93       if (attachedToProgress) {
    94         Progress.Hide(this);
    95         attachedToProgress = false;
    96       }
    9799    }
    98100
     
    133135      variableImpactsArrayView.Caption = Content.Name + " Variable Impacts";
    134136      progress = Progress.Show(this, "Calculating variable impacts for " + Content.Name);
    135       attachedToProgress = true;
    136137      cancellationToken = new CancellationTokenSource();
    137138
     
    145146          .ToList();
    146147
    147         List<Tuple<string, double>> impacts = null;
    148         await Task.Run(() => { impacts = CalculateVariableImpacts(originalVariableOrdering, Content.Model, problemData, Content.EstimatedClassValues, dataPartition, replMethod, factorReplMethod, cancellationToken.Token, progress); });
    149         if (impacts == null) { return; }
     148        var impacts = await Task.Run(() => CalculateVariableImpacts(originalVariableOrdering, Content.Model, problemData, Content.EstimatedClassValues, dataPartition, replMethod, factorReplMethod, cancellationToken.Token, progress));
    150149
    151150        rawVariableImpacts.AddRange(impacts);
    152151        UpdateOrdering();
     152      } catch (OperationCanceledException) {
    153153      } finally {
    154         if (attachedToProgress) {
    155           Progress.Hide(this);
    156           attachedToProgress = false;
    157         }
     154        Progress.Hide(this);
    158155      }
    159156    }
     
    180177      var clonedModel = (IClassificationModel)model.Clone();
    181178      foreach (var variableName in originalVariableOrdering) {
    182         if (cancellationToken.Token.IsCancellationRequested) { return null; }
     179        token.ThrowIfCancellationRequested();
    183180        progress.ProgressValue = (double)++i / count;
    184181        progress.Message = string.Format("Calculating impact for variable {0} ({1} of {2})", variableName, i, count);
  • trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs

    r17276 r17426  
    2525using System.Threading;
    2626using System.Threading.Tasks;
     27using System.Windows.Forms;
    2728using HeuristicLab.Common;
    2829using HeuristicLab.Data;
     
    4041    private CancellationTokenSource cancellationToken = new CancellationTokenSource();
    4142    private List<Tuple<string, double>> rawVariableImpacts = new List<Tuple<string, double>>();
    42     private bool attachedToProgress = false;
    4343
    4444    public new IRegressionSolution Content {
     
    8888    }
    8989
    90     protected override void OnHidden(EventArgs e) {
    91       base.OnHidden(e);
     90    protected override void OnVisibleChanged(EventArgs e) {
     91      base.OnVisibleChanged(e);
     92      if (!this.Visible) {
     93        cancellationToken.Cancel();
     94      }
     95    }
     96
     97    protected override void OnClosed(FormClosedEventArgs e) {
     98      base.OnClosed(e);
    9299      cancellationToken.Cancel();
    93 
    94       if (attachedToProgress) {
    95         Progress.Hide(this);
    96         attachedToProgress = false;
    97       }
    98100    }
    99101
     
    132134      variableImpactsArrayView.Caption = Content.Name + " Variable Impacts";
    133135      var progress = Progress.Show(this, "Calculating variable impacts for " + Content.Name);
    134       attachedToProgress = true;
    135136      cancellationToken = new CancellationTokenSource();
    136137
     
    144145          .ToList();
    145146
    146         List<Tuple<string, double>> impacts = null;
    147         await Task.Run(() => { impacts = CalculateVariableImpacts(originalVariableOrdering, Content.Model, problemData, Content.EstimatedValues, dataPartition, replMethod, factorReplMethod, cancellationToken.Token, progress); });
    148         if (impacts == null) { return; }
     147        var impacts = await Task.Run(() => CalculateVariableImpacts(originalVariableOrdering, Content.Model, problemData, Content.EstimatedValues, dataPartition, replMethod, factorReplMethod, cancellationToken.Token, progress));
    149148
    150149        rawVariableImpacts.AddRange(impacts);
    151150        UpdateOrdering();
     151      } catch (OperationCanceledException) {
    152152      } finally {
    153         if (attachedToProgress) {
    154           Progress.Hide(this);
    155           attachedToProgress = false;
    156         }
     153        Progress.Hide(this);
    157154      }
    158155    }
     
    179176
    180177      foreach (var variableName in originalVariableOrdering) {
    181         if (cancellationToken.Token.IsCancellationRequested) { return null; }
     178        token.ThrowIfCancellationRequested();
    182179        progress.ProgressValue = (double)++i / count;
    183180        progress.Message = string.Format("Calculating impact for variable {0} ({1} of {2})", variableName, i, count);
Note: See TracChangeset for help on using the changeset viewer.