Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs @ 17717

Last change on this file since 17717 was 17646, checked in by gkronber, 4 years ago

#3078 added synchronization in RegressionInstanceProviderView which solves the crashes on my machine.

File size: 7.7 KB
RevLine 
[8187]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8187]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Windows.Forms;
24
25namespace HeuristicLab.MainForm.WindowsForms {
[9894]26  internal sealed partial class ProgressView : UserControl {
[17426]27    public Control TargetControl { get; }
28    public IProgress Content { get; }
[8187]29
[17426]30    public ProgressView(Control targetControl, IProgress content)
[9893]31      : base() {
[17426]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));
[8187]35      InitializeComponent();
[9868]36
[17426]37      this.TargetControl = targetControl;
38      this.Content = content;
[16430]39
40      if (content.ProgressState != ProgressState.Finished)
[9893]41        ShowProgress();
42      RegisterContentEvents();
[8187]43    }
44
[9894]45    protected override void Dispose(bool disposing) {
46      DeregisterContentEvents();
47
[17426]48      if (!TargetControl.IsDisposed)
49        HideProgress();
50
51      if (disposing && components != null) {
[9894]52        components.Dispose();
53      }
54      base.Dispose(disposing);
55    }
56
[9893]57    private void RegisterContentEvents() {
[16430]58      Content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
59      Content.MessageChanged += new EventHandler(Content_MessageChanged);
60      Content.ProgressBarModeChanged += new EventHandler(Content_ProgressBarModeChanged);
61      Content.ProgressValueChanged += new EventHandler(Content_ProgressValueChanged);
62      Content.CanBeStoppedChanged += new EventHandler(Content_CanBeStoppedChanged);
63      Content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
[9849]64    }
[9893]65    private void DeregisterContentEvents() {
[16430]66      Content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
67      Content.MessageChanged -= new EventHandler(Content_MessageChanged);
68      Content.ProgressBarModeChanged -= new EventHandler(Content_ProgressBarModeChanged);
69      Content.ProgressValueChanged -= new EventHandler(Content_ProgressValueChanged);
70      Content.CanBeStoppedChanged -= new EventHandler(Content_CanBeStoppedChanged);
71      Content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
[8187]72    }
73
[16430]74    private void Content_ProgressStateChanged(object sender, EventArgs e) {
75      UpdateProgressState();
76      UpdateButtonsState();
77    }
78
79    private void Content_MessageChanged(object sender, EventArgs e) {
80      UpdateProgressMessage();
81    }
82
83    private void Content_ProgressBarModeChanged(object sender, EventArgs e) {
84      UpdateProgressValue();
85    }
86    private void Content_ProgressValueChanged(object sender, EventArgs e) {
87      UpdateProgressValue();
88    }
89
90    private void Content_CanBeStoppedChanged(object sender, EventArgs e) {
91      UpdateButtonsState();
92    }
93    private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
94      UpdateButtonsState();
95    }
96
[9893]97    private void ShowProgress() {
[17426]98      if (TargetControl.InvokeRequired) {
99        TargetControl.Invoke((Action)ShowProgress);
[9893]100        return;
[8187]101      }
[16430]102      if (Parent != null) return;
[9907]103
[17426]104      Left = (TargetControl.ClientRectangle.Width / 2) - (Width / 2);
105      Top = (TargetControl.ClientRectangle.Height / 2) - (Height / 2);
[9893]106      Anchor = AnchorStyles.None;
[8187]107
[16430]108      UpdateProgressMessage();
109      UpdateProgressValue();
110      UpdateButtonsState();
111
[17426]112      TargetControl.SuspendRepaint();
113      TargetControl.Enabled = false;
114      RegisterTargetControlEvents();
115      Parent = TargetControl.Parent;
[9893]116      BringToFront();
[17426]117      TargetControl.ResumeRepaint(true);
[9893]118      Visible = true;
[8187]119    }
120
121    private void HideProgress() {
[17426]122      if (TargetControl.InvokeRequired) {
123        TargetControl.Invoke((Action)HideProgress);
[16430]124        return;
[8187]125      }
[16512]126      if (Parent == null) return;
[8187]127
[16430]128      Visible = false;
[17426]129      TargetControl.SuspendRepaint();
130      TargetControl.Enabled = true;
131      DeregisterTargetControlEvents();
[16430]132      Parent = null;
[17426]133      TargetControl.ResumeRepaint(TargetControl.Visible);
[8187]134    }
135
[17426]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;
157    }
158
[16430]159    private void UpdateProgressState() {
[17426]160      if (TargetControl.InvokeRequired) {
161        TargetControl.Invoke((Action)UpdateProgressState);
[16430]162        return;
163      }
164
165      if (Content.ProgressState != ProgressState.Finished)
166        ShowProgress();
167      else
168        HideProgress();
[8187]169    }
170
[16430]171    private void UpdateProgressMessage() {
[17426]172      if (TargetControl.InvokeRequired) {
173        TargetControl.Invoke((Action)UpdateProgressMessage);
[16430]174        return;
[9849]175      }
[8187]176
[17426]177      messageLabel.Text = Content.Message;
[8187]178    }
179
[16430]180    private void UpdateProgressValue() {
[17646]181      if (Disposing || IsDisposed) return;
[16430]182      if (InvokeRequired) {
183        Invoke((Action)UpdateProgressValue);
184        return;
185      }
[9907]186
[16430]187      switch (Content.ProgressMode) {
188        case ProgressMode.Determinate:
189          progressBar.Style = ProgressBarStyle.Continuous;
[17426]190          progressBar.Value = (int)Math.Round(progressBar.Minimum + Content.ProgressValue * (progressBar.Maximum - progressBar.Minimum));
[16430]191          break;
192        case ProgressMode.Indeterminate:
193          progressBar.Style = ProgressBarStyle.Marquee;
194          progressBar.Value = 0;
195          break;
196        default:
[17426]197          throw new NotImplementedException($"Invalid Progress Mode: {Content.ProgressMode}");
[9907]198      }
[8187]199    }
200
[16430]201    private void UpdateButtonsState() {
[17426]202      if (TargetControl.InvokeRequired) {
203        TargetControl.Invoke((Action)UpdateButtonsState);
[16430]204        return;
[8187]205      }
[16430]206
207      stopButton.Visible = Content.CanBeStopped;
[17426]208      stopButton.Enabled = Content.CanBeStopped && Content.ProgressState == ProgressState.Started;
[16430]209
210      cancelButton.Visible = Content.CanBeCanceled;
[17426]211      cancelButton.Enabled = Content.CanBeCanceled && Content.ProgressState == ProgressState.Started;
[8187]212    }
213
[16430]214    private void stopButton_Click(object sender, EventArgs e) {
215      Content.Stop();
[8187]216    }
217    private void cancelButton_Click(object sender, EventArgs e) {
[16430]218      Content.Cancel();
[8187]219    }
220  }
221}
Note: See TracBrowser for help on using the repository browser.