1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Threading.Tasks;
|
---|
24 | using System.Windows.Forms;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Clients.Access {
|
---|
27 | public partial class ClientRegistrationDialog : Form {
|
---|
28 | public ClientRegistrationDialog() {
|
---|
29 | InitializeComponent();
|
---|
30 | }
|
---|
31 |
|
---|
32 | private void btnCollectInformation_Click(object sender, EventArgs e) {
|
---|
33 | progressBar.Visible = true;
|
---|
34 | progressBar.Style = ProgressBarStyle.Marquee;
|
---|
35 | btnCollectInformation.Enabled = false;
|
---|
36 | btnRegisterClient.Enabled = false;
|
---|
37 |
|
---|
38 | Task<Client> task = Task.Factory.StartNew<Client>(ClientInformationUtils.CollectClientInformation);
|
---|
39 | task.ContinueWith(c => DisplayContent(c.Result));
|
---|
40 | }
|
---|
41 |
|
---|
42 | private void DisplayContent(Client client) {
|
---|
43 | if (!this.Disposing) {
|
---|
44 | if (this.InvokeRequired) {
|
---|
45 | Invoke(new Action<Client>(DisplayContent), client);
|
---|
46 | } else {
|
---|
47 | clientView.Content = client;
|
---|
48 | progressBar.Visible = false;
|
---|
49 | btnCollectInformation.Enabled = true;
|
---|
50 | btnRegisterClient.Enabled = true;
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | private void AddClient() {
|
---|
56 | AccessClient.CallAccessService(x => x.AddClient(clientView.Content));
|
---|
57 | }
|
---|
58 |
|
---|
59 | private void RefreshClientInformation(Task task) {
|
---|
60 | ClientInformation.Instance.Refresh();
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void btnRegisterClient_Click(object sender, EventArgs e) {
|
---|
64 | progressBar.Visible = true;
|
---|
65 | btnRegisterClient.Enabled = false;
|
---|
66 | btnCollectInformation.Enabled = false;
|
---|
67 | btnCancel.Enabled = false;
|
---|
68 |
|
---|
69 | Task task = Task.Factory.StartNew(AddClient);
|
---|
70 | task.ContinueWith(RefreshClientInformation, TaskContinuationOptions.NotOnFaulted);
|
---|
71 | task.ContinueWith(FinishRegistration, TaskContinuationOptions.NotOnFaulted);
|
---|
72 | task.ContinueWith(HandleRegistrationError, TaskContinuationOptions.OnlyOnFaulted);
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void FinishRegistration(Task task) {
|
---|
76 | if (!this.Disposing) {
|
---|
77 | if (this.InvokeRequired) {
|
---|
78 | Invoke(new Action<Task>(FinishRegistration), task);
|
---|
79 | } else {
|
---|
80 | progressBar.Visible = false;
|
---|
81 | btnCollectInformation.Enabled = true;
|
---|
82 | btnCancel.Enabled = true;
|
---|
83 | MessageBox.Show("Your HeuristicLab client has been registered successfully.", "HeuristicLab Registration", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | private void HandleRegistrationError(Task task) {
|
---|
89 | if (!this.Disposing) {
|
---|
90 | if (this.InvokeRequired) {
|
---|
91 | Invoke(new Action<Task>(HandleRegistrationError), task);
|
---|
92 | } else {
|
---|
93 | progressBar.Visible = false;
|
---|
94 | btnRegisterClient.Enabled = true;
|
---|
95 | btnCollectInformation.Enabled = true;
|
---|
96 | btnCancel.Enabled = true;
|
---|
97 | PluginInfrastructure.ErrorHandling.ShowErrorDialog(task.Exception);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|