Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Hive.JobManager/3.3/Views/HiveJobManagerView.cs @ 15419

Last change on this file since 15419 was 15419, checked in by pfleck, 7 years ago

#2846: Fixed uncaught ObjectDisposedException in HiveJobManagerView.

  • Caught a potential NullReferenceException in the HiveClient when ClearHiveClient is called asynchronous.
  • Use Task.Run for HiveClient.Refresh instead of Begin/EndInvoke.
  • Use await with try-catch to marshal back any uncaught exceptions back to the UI-thread where the exception is displayed instead of HL crashing.
File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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.Linq;
24using System.ServiceModel.Security;
25using System.Windows.Forms;
26using HeuristicLab.Clients.Hive.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Clients.Hive.JobManager.Views {
32  /// <summary>
33  /// The base class for visual representations of items.
34  /// </summary>
35  [View("Hive Job Manager")]
36  [Content(typeof(HiveClient), true)]
37  public partial class HiveJobManagerView : AsynchronousContentView {
38
39    public new HiveClient Content {
40      get { return (HiveClient)base.Content; }
41      set { base.Content = value; }
42    }
43
44    /// <summary>
45    /// Initializes a new instance of <see cref="ItemBaseView"/>.
46    /// </summary>
47    public HiveJobManagerView() {
48      InitializeComponent();
49    }
50
51    protected override void RegisterContentEvents() {
52      base.RegisterContentEvents();
53      Content.Refreshing += new EventHandler(Content_Refreshing);
54      Content.Refreshed += new EventHandler(Content_Refreshed);
55    }
56
57    protected override void DeregisterContentEvents() {
58      Content.Refreshing -= new EventHandler(Content_Refreshing);
59      Content.Refreshed -= new EventHandler(Content_Refreshed);
60      base.DeregisterContentEvents();
61    }
62
63    protected async override void OnContentChanged() {
64      base.OnContentChanged();
65      if (Content == null) {
66        hiveExperimentListView.Content = null;
67      } else {
68        hiveExperimentListView.Content = Content.Jobs;
69        if (Content != null) {
70          try {
71            await System.Threading.Tasks.Task.Run(() => Content.Refresh());
72          } catch (Exception ex) {
73            HandleServiceException(ex);
74          }
75        }
76      }
77    }
78
79    protected override void SetEnabledStateOfControls() {
80      base.SetEnabledStateOfControls();
81      refreshButton.Enabled = Content != null;
82      hiveExperimentListView.Enabled = Content != null;
83    }
84
85    private void Content_Refreshing(object sender, EventArgs e) {
86      if (InvokeRequired) {
87        Invoke(new EventHandler(Content_Refreshing), sender, e);
88      } else {
89        Cursor = Cursors.AppStarting;
90        refreshButton.Enabled = false;
91        hiveExperimentListView.Enabled = false;
92      }
93    }
94    private void Content_Refreshed(object sender, EventArgs e) {
95      if (InvokeRequired) {
96        Invoke(new EventHandler(Content_Refreshed), sender, e);
97      } else {
98        hiveExperimentListView.Content = Content.Jobs;
99        refreshButton.Enabled = true;
100        hiveExperimentListView.Enabled = true;
101        Cursor = Cursors.Default;
102      }
103    }
104
105    private async void refreshButton_Click(object sender, EventArgs e) {
106      try {
107        await System.Threading.Tasks.Task.Run(() => Content.Refresh());
108      } catch (Exception ex) {
109        HandleServiceException(ex);
110      }
111    }
112
113    private void HandleServiceException(Exception ex) {
114      if (this.InvokeRequired) {
115        Invoke(new Action<Exception>(HandleServiceException), ex);
116      } else {
117        if (ex is MessageSecurityException) {
118          string message =
119            "A Message Security error has occured. This normally means that your user name or password is wrong. Detailed information: " + Environment.NewLine;
120          message += HiveServiceLocator.Instance.GetEndpointInformation();
121          ErrorHandling.ShowErrorDialog(this, message, ex);
122        } else if (ex is AnonymousUserException) {
123          using (HiveInformationDialog dialog = new HiveInformationDialog()) {
124            dialog.ShowDialog(this);
125          }
126        } else {
127          ErrorHandling.ShowErrorDialog(this,
128            "Refresh failed. Detailed information: " + Environment.NewLine + HiveServiceLocator.Instance.GetEndpointInformation(), ex);
129        }
130      }
131    }
132
133    protected override void OnClosing(FormClosingEventArgs e) {
134      if (Content != null && Content.Jobs != null && Content.Jobs.Any(x => x.IsProgressing)) {
135        DialogResult result = MessageBox.Show("There are still unfinished down/uploads. Are you sure you want to close the window?", "HeuristicLab Hive Job Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
136
137        if (result == DialogResult.No) {
138          e.Cancel = true;
139        }
140      } else {
141        base.OnClosing(e);
142      }
143    }
144
145    protected override void OnClosed(FormClosedEventArgs e) {
146      if (Content != null && Content.Jobs != null) {
147        Content.ClearHiveClient();
148        Content = null;
149      }
150      base.OnClosed(e);
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.