source: trunk/sources/HeuristicLab/MainForm.cs @ 1392

Last change on this file since 1392 was 1392, checked in by gkronber, 12 years ago

Fixed #530 (Service applications are restarted again and again even when the user terminates the starter)

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Text;
28using System.Windows.Forms;
29using System.Diagnostics;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.PluginInfrastructure.GUI;
32using System.Threading;
33
34namespace HeuristicLab {
35  public partial class MainForm : Form {
36
37    private ListViewItem pluginManagerListViewItem;
38    private bool abortRequested;
39
40    public MainForm() {
41      abortRequested = false;
42      SplashScreen splashScreen = new SplashScreen(1000, "Loading HeuristicLab...");
43      splashScreen.Owner = this;
44      splashScreen.Show();
45
46      Application.DoEvents();
47      this.Enabled = false;
48
49      PluginManager.Manager.Action += new PluginManagerActionEventHandler(splashScreen.Manager_Action);
50      PluginManager.Manager.Initialize();
51
52      InitializeComponent();
53
54      RefreshApplicationsList();
55
56      this.Enabled = true;
57      this.Visible = true;
58    }
59
60    private void RefreshApplicationsList() {
61      applicationsListView.Items.Clear();
62
63      pluginManagerListViewItem = new ListViewItem("Plugin Manager", 0);
64      pluginManagerListViewItem.Group = applicationsListView.Groups["Plugin Management"];
65      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "-"));
66      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "Install, upgrade or delete plugins"));
67      pluginManagerListViewItem.ToolTipText = "Install, upgrade or delete plugins";
68
69      applicationsListView.Items.Add(pluginManagerListViewItem);
70
71      foreach(ApplicationInfo info in PluginManager.Manager.InstalledApplications) {
72        ListViewItem item = new ListViewItem(info.Name, 0);
73        item.Tag = info;
74        item.Group = applicationsListView.Groups["Applications"];
75        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Version.ToString()));
76        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Description));
77        item.ToolTipText = info.Description;
78        applicationsListView.Items.Add(item);
79      }
80    }
81
82    private void applicationsListView_ItemActivate(object sender, EventArgs e) {
83      if(applicationsListView.SelectedItems.Count > 0) {
84        ListViewItem selected = applicationsListView.SelectedItems[0];
85        if(selected == pluginManagerListViewItem) {
86          ManagerForm form = new ManagerForm();
87          this.Visible = false;
88          form.ShowDialog(this);
89          RefreshApplicationsList();
90          this.Visible = true;
91        } else {
92          ApplicationInfo app = (ApplicationInfo)applicationsListView.SelectedItems[0].Tag;
93          SplashScreen splashScreen = new SplashScreen(2000, "Loading " + app.Name);
94          splashScreen.Owner = this;
95          splashScreen.Show();
96          PluginManager.Manager.Action += new PluginManagerActionEventHandler(splashScreen.Manager_Action);
97          Thread t = new Thread(delegate() {
98            bool stopped = false;
99            do {
100              try {
101                if(!abortRequested)
102                  PluginManager.Manager.Run(app);
103                stopped = true;
104              } catch(Exception ex) {
105                stopped = false;
106                ThreadPool.QueueUserWorkItem(delegate(object exception) { ShowErrorMessageBox((Exception)exception); }, ex);
107                Thread.Sleep(5000); // sleep 5 seconds before autorestart
108              }
109            } while(!abortRequested && !stopped && app.AutoRestart);
110          });
111          t.SetApartmentState(ApartmentState.STA); // needed for the AdvancedOptimizationFrontent
112          t.Start();
113        }
114      }
115    }
116
117    private void applicationsListBox_SelectedIndexChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
118      if(e.IsSelected) {
119        startButton.Enabled = true;
120      } else {
121        startButton.Enabled = false;
122      }
123    }
124
125    private void largeIconsButton_Click(object sender, EventArgs e) {
126      applicationsListView.View = View.LargeIcon;
127    }
128
129    private void listButton_Click(object sender, EventArgs e) {
130      applicationsListView.View = View.List;
131    }
132
133    private void detailsButton_Click(object sender, EventArgs e) {
134      applicationsListView.View = View.Details;
135    }
136
137    public void ShowErrorMessageBox(Exception ex) {
138      MessageBox.Show(BuildErrorMessage(ex),
139                      "Error - " + ex.GetType().Name,
140                      MessageBoxButtons.OK,
141                      MessageBoxIcon.Error);
142    }
143    private string BuildErrorMessage(Exception ex) {
144      StringBuilder sb = new StringBuilder();
145      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
146
147      while(ex.InnerException != null) {
148        ex = ex.InnerException;
149        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
150      }
151      return sb.ToString();
152    }
153
154    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
155      abortRequested = true;
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.