Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Starter/StarterForm.cs @ 4892

Last change on this file since 4892 was 4515, checked in by gkronber, 14 years ago

Added work-around for race-condition in SplashScreen:

  • deregister events correctly
  • catch ObjectDisposedException in UpdateMessage in splash-screen

#795

File size: 8.0 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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.Diagnostics;
[4068]25using System.IO;
26using System.Linq;
[2]27using System.Threading;
[4068]28using System.Windows.Forms;
29using HeuristicLab.PluginInfrastructure.Advanced;
[2481]30using HeuristicLab.PluginInfrastructure.Manager;
[2]31
[2527]32namespace HeuristicLab.PluginInfrastructure.Starter {
[3092]33  /// <summary>
34  /// The starter form is responsible for initializing the plugin infrastructure
35  /// and shows a list of installed applications.
36  /// </summary>
[2507]37  public partial class StarterForm : Form {
[2]38
39    private ListViewItem pluginManagerListViewItem;
[1392]40    private bool abortRequested;
[2488]41    private PluginManager pluginManager;
[3113]42    private SplashScreen splashScreen;
[2]43
[3092]44    /// <summary>
45    /// Initializes an instance of the starter form.
46    /// The starter form shows a splashscreen and initializes the plugin infrastructure.
47    /// </summary>
[2507]48    public StarterForm()
[2481]49      : base() {
50      InitializeComponent();
[3748]51      largeImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.HeuristicLab.ToBitmap());
52      smallImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.HeuristicLab.ToBitmap());
[3600]53      FileVersionInfo pluginInfrastructureVersion = FileVersionInfo.GetVersionInfo(GetType().Assembly.Location);
54      Text = "HeuristicLab " + pluginInfrastructureVersion.FileVersion;
[2481]55
[2748]56      string pluginPath = Path.GetFullPath(Application.StartupPath);
[2495]57      pluginManager = new PluginManager(pluginPath);
[3113]58      splashScreen = new SplashScreen(pluginManager, 1000);
[3736]59      splashScreen.Show(this, "Loading HeuristicLab...");
[2]60
[2497]61      pluginManager.DiscoverAndCheckPlugins();
[3474]62      UpdateApplicationsList();
[2]63    }
64
[3092]65    /// <summary>
66    /// Creates a new StarterForm and tries to start application with <paramref name="appName"/> immediately.
67    /// </summary>
68    /// <param name="appName">Name of the application</param>
[2507]69    public StarterForm(string appName)
70      : this() {
71      var appDesc = (from desc in pluginManager.Applications
72                     where desc.Name == appName
[2748]73                     select desc).SingleOrDefault();
[2507]74      if (appDesc != null) {
75        StartApplication(appDesc);
76      } else {
77        MessageBox.Show("Cannot start application " + appName + ".",
78                        "HeuristicLab",
79                        MessageBoxButtons.OK,
80                        MessageBoxIcon.Warning);
81      }
82    }
83
[2]84    private void applicationsListView_ItemActivate(object sender, EventArgs e) {
[1394]85      if (applicationsListView.SelectedItems.Count > 0) {
[2]86        ListViewItem selected = applicationsListView.SelectedItems[0];
[1394]87        if (selected == pluginManagerListViewItem) {
[2922]88          if (pluginManager.Plugins.Any(x => x.PluginState == PluginState.Loaded)) {
89            MessageBox.Show("Installation Manager cannot be started while another HeuristicLab application is active." + Environment.NewLine +
[3573]90              "Please stop all active HeuristicLab applications and try again.", "Plugin Manager",
91              MessageBoxButtons.OK, MessageBoxIcon.Information);
[2922]92          } else {
93            try {
94              Cursor = Cursors.AppStarting;
[4482]95              using (InstallationManagerForm form = new InstallationManagerForm(pluginManager)) {
96                form.ShowDialog(this);
97              }
[3474]98              UpdateApplicationsList();
[2922]99            }
100            finally {
101              Cursor = Cursors.Arrow;
102            }
[1394]103          }
[2]104        } else {
[2481]105          ApplicationDescription app = (ApplicationDescription)applicationsListView.SelectedItems[0].Tag;
[2507]106          StartApplication(app);
[2]107        }
108      }
109    }
110
[3474]111    private void UpdateApplicationsList() {
112      applicationsListView.Items.Clear();
[3600]113      FileVersionInfo pluginInfrastructureVersion = FileVersionInfo.GetVersionInfo(GetType().Assembly.Location);
[3474]114      pluginManagerListViewItem = new ListViewItem("Plugin Manager", 0);
115      pluginManagerListViewItem.Group = applicationsListView.Groups["Plugin Management"];
[3600]116      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, pluginInfrastructureVersion.FileVersion));
[3474]117      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "Install, upgrade or delete plugins"));
118      pluginManagerListViewItem.ToolTipText = "Install, upgrade or delete plugins";
119
120      applicationsListView.Items.Add(pluginManagerListViewItem);
121
122      foreach (ApplicationDescription info in pluginManager.Applications) {
123        ListViewItem item = new ListViewItem(info.Name, 0);
124        item.Tag = info;
125        item.Group = applicationsListView.Groups["Applications"];
126        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Version.ToString()));
127        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Description));
128        item.ToolTipText = info.Description;
129        applicationsListView.Items.Add(item);
130      }
[3600]131      foreach (ColumnHeader column in applicationsListView.Columns) {
132        if (applicationsListView.Items.Count > 0)
133          column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
134        else column.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
135      }
[3474]136    }
137
[2507]138    private void StartApplication(ApplicationDescription app) {
[3113]139      splashScreen.Show("Loading " + app.Name);
[2507]140      Thread t = new Thread(delegate() {
141        bool stopped = false;
142        do {
143          try {
[2922]144            if (!abortRequested) {
[2507]145              pluginManager.Run(app);
[2922]146            }
[2507]147            stopped = true;
148          }
149          catch (Exception ex) {
150            stopped = false;
[3758]151            ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
[2507]152            Thread.Sleep(5000); // sleep 5 seconds before autorestart
153          }
154        } while (!abortRequested && !stopped && app.AutoRestart);
155      });
156      t.SetApartmentState(ApartmentState.STA); // needed for the AdvancedOptimizationFrontent
157      t.Start();
158    }
159
[3573]160    private void applicationsListView_SelectedIndexChanged(object sender, EventArgs e) {
161      startButton.Enabled = applicationsListView.SelectedItems.Count > 0;
[2922]162    }
163
[2]164    private void largeIconsButton_Click(object sender, EventArgs e) {
165      applicationsListView.View = View.LargeIcon;
166    }
167
168    private void detailsButton_Click(object sender, EventArgs e) {
169      applicationsListView.View = View.Details;
[3600]170      foreach (ColumnHeader column in applicationsListView.Columns) {
171        if (applicationsListView.Items.Count > 0)
172          column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
173        else column.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
174      }
[2]175    }
176
[1392]177    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
[4515]178      splashScreen.Close();
[1392]179      abortRequested = true;
180    }
[3573]181
[3736]182    private void aboutButton_Click(object sender, EventArgs e) {
183      List<IPluginDescription> plugins = new List<IPluginDescription>(pluginManager.Plugins.OfType<IPluginDescription>());
[4482]184      using (var dialog = new AboutDialog(plugins)) {
185        dialog.ShowDialog();
186      }
[3736]187    }
[2]188  }
189}
Note: See TracBrowser for help on using the repository browser.