Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 302 was 242, checked in by gkronber, 17 years ago

added support for "service" applications that are restarted automatically when they crash with an exception. (related to #149)

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