Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Clients.Hive.CloudManager/3.3/Views/AddAzureServiceDialog.cs @ 7357

Last change on this file since 7357 was 7357, checked in by spimming, 12 years ago

#1680:

  • removed unnecessary invoke-calls
  • moved method call to disable controls out of the worker
File size: 6.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Windows.Forms;
5using HeuristicLab.Clients.Hive.CloudManager.Model;
6using HeuristicLab.Core;
7
8namespace HeuristicLab.Clients.Hive.CloudManager.Views {
9  public partial class AddAzureServiceDialog : Form {
10    private BindingList<Subscription> subscriptions;
11    private BindingList<HostedService> hostedServices;
12    private BindingList<string> locations;
13    private BindingList<AffinityGroup> affinityGroups;
14
15    private BackgroundWorker worker;
16
17    private bool isInitializing;
18    private bool bwCompleted;
19    private bool closePending;
20
21    public AddAzureServiceDialog(IItemList<Subscription> subs) {
22      InitializeComponent();
23      isInitializing = true;
24      bwCompleted = true;
25
26      this.subscriptions = new BindingList<Subscription>(subs);
27      this.hostedServices = new BindingList<HostedService>();
28      this.locations = new BindingList<string>();
29      this.affinityGroups = new BindingList<AffinityGroup>();
30
31      SetLookupBinding(cmbLocation, locations, "", "");
32      SetLookupBinding(cmbAffinityGroup, affinityGroups, "Label", "Name");
33      SetLookupBinding(cmbChooseHostedService, hostedServices, "ServiceName", "ServiceName");
34      SetLookupBinding(cmbChooseSubscription, subscriptions, "SubscriptionName", "SubscriptionID");
35
36      if (!cbNewHostedService.Checked) {
37        SetGroupBoxControlsEnabled(gbNewHostedService, cbNewHostedService.Checked);
38      }
39
40      worker = new BackgroundWorker();
41      worker.DoWork += new DoWorkEventHandler(UpdateTask);
42      worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
43
44
45      isInitializing = false;
46      cmbChooseSubscription_SelectedValueChanged(this, EventArgs.Empty);
47    }
48
49    protected override void OnFormClosing(FormClosingEventArgs e) {
50      if (!bwCompleted) {
51        SetAllControlsEnabled(this, false);
52        e.Cancel = true;
53        closePending = true;
54      }
55    }
56
57    private void SetLookupBinding(ListControl toBind, object dataSource, string displayMember, string valueMember) {
58      toBind.DisplayMember = displayMember;
59      toBind.ValueMember = valueMember;
60      toBind.DataSource = dataSource;
61    }
62
63    private void SetGroupBoxControlsEnabled(GroupBox gb, bool isEnabled) {
64      foreach (Control ctrl in gb.Controls) {
65        ctrl.Enabled = isEnabled;
66      }
67    }
68
69    private void SetAllControlsEnabled(Form frm, bool isEnabled) {
70      foreach (Control ctrl in frm.Controls) {
71        if (!(ctrl is ProgressBar)) {
72          ctrl.Enabled = isEnabled;
73        }
74      }
75    }
76
77    private void SetBindingList<T>(ref BindingList<T> bindinglist, List<T> list) {
78      if (list != null) {
79        BindingList<T> newList = new BindingList<T>(list);
80        bindinglist = newList;
81      }
82    }
83
84    private void UpdateBindingList<T>(BindingList<T> bindinglist, List<T> list) {
85      if (list != null) {
86        bindinglist.Clear();
87        foreach (T item in list)
88          bindinglist.Add(item);
89      }
90    }
91
92    private void cbNewHostedService_CheckedChanged(object sender, EventArgs e) {
93      if (cbNewHostedService.Checked) {
94        Subscription subscription = (Subscription)cmbChooseSubscription.SelectedItem;
95        if (subscription.CurrentHostedServices == subscription.MaxHostedServices) {
96          MessageBox.Show("You have already created the maximum number of hosted services", "Limit Reached", MessageBoxButtons.OK, MessageBoxIcon.Error);
97          cbNewHostedService.Checked = false;
98        } else {
99          SetGroupBoxControlsEnabled(gbNewHostedService, cbNewHostedService.Checked);
100          cmbChooseHostedService.Enabled = false;
101        }
102      } else {
103        // checked == false
104        SetGroupBoxControlsEnabled(gbNewHostedService, false);
105        if (hostedServices.Count == 0) {
106          cbNewHostedService.Checked = true;
107        }
108      }
109    }
110
111    private void cmbChooseSubscription_SelectedValueChanged(object sender, EventArgs e) {
112      if (!isInitializing) {
113        Subscription item = (Subscription)cmbChooseSubscription.SelectedItem;
114        if (item != null) {
115          bwCompleted = false;
116          progressBar.Visible = true;
117          SetAllControlsEnabled(this, false);
118          worker.RunWorkerAsync(item);
119        }
120      }
121    }
122
123    private void btnAddCertificate_Click(object sender, EventArgs e) {
124      using (var form = new AddCertificate()) {
125        form.ShowDialog();
126      }
127    }
128
129    private void btnCancel_Click(object sender, EventArgs e) {
130      this.Close();
131    }
132
133    private void btnOk_Click(object sender, EventArgs e) {
134      if (cmbChooseSubscription.SelectedItem != null) {
135        if (cbNewHostedService.Checked) {
136          if (rbRegion.Checked) {
137            string serviceName = tbServiceName.Text;
138            string label = tbLabel.Text;
139            string location = (string)cmbLocation.SelectedItem;
140            Subscription sub = (Subscription)cmbChooseSubscription.SelectedItem;
141            CloudManagerClient.Instance.AzureProvider.CreateHostedService(sub, serviceName, label, "Hosted service for hive slave", location);
142          } else {
143            // rbAffinitGroup.Checked
144          }
145        }
146      }
147    }
148
149    private void UpdateTask(object sender, DoWorkEventArgs e) {
150      try {
151        Subscription item = (Subscription)e.Argument;
152        item = CloudManagerClient.Instance.AzureProvider.GetSubscriptionInfo(item.SubscriptionID, item.CertificateThumbprint);
153        this.Invoke((MethodInvoker)delegate { lblCores.Text = item.CurrentCoreCount + " / " + item.MaxCoreCount; });
154
155        List<HostedService> services = CloudManagerClient.Instance.AzureProvider.ListHostedServices(item);
156        List<string> locs = CloudManagerClient.Instance.AzureProvider.ListLocations(item);
157        List<AffinityGroup> groups = CloudManagerClient.Instance.AzureProvider.ListAffinityGroups(item);
158
159        this.Invoke((MethodInvoker)delegate { UpdateBindingList<HostedService>(hostedServices, services); });
160        this.Invoke((MethodInvoker)delegate { UpdateBindingList<string>(locations, locs); });
161        this.Invoke((MethodInvoker)delegate { UpdateBindingList<AffinityGroup>(affinityGroups, groups); });
162      }
163      catch (Exception ex) {
164        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
165      }
166    }
167
168    private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
169      SetAllControlsEnabled(this, true);
170      progressBar.Visible = false;
171      bwCompleted = true;
172      if (closePending) {
173        this.Close();
174      }
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.