Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RefactorPluginInfrastructure-2522/HeuristicLab/3.3/Program.cs @ 13389

Last change on this file since 13389 was 13389, checked in by gkronber, 8 years ago

#2522: removed Starter form and instead init plugin discovery and launch of application from Startup project (.exe)

File size: 3.6 KB
RevLine 
[2492]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2492]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;
[13389]23using System.IO;
24using System.Linq;
25using System.Threading;
26using System.Threading.Tasks;
[13338]27using System.Windows.Forms;
[13389]28using HeuristicLab.PluginInfrastructure;
[13338]29using HeuristicLab.PluginInfrastructure.UI;
[2492]30
[2643]31namespace HeuristicLab {
[2492]32  static class Program {
33    [STAThread]
[13338]34    /// <summary>
35    /// Main entry point of the plugin infrastructure. Loads the starter form.
36    /// </summary>
37    /// <param name="args">Command line arguments</param>
[2492]38    static void Main(string[] args) {
[13338]39      if ((!FrameworkVersionErrorDialog.NET4_5Installed && !FrameworkVersionErrorDialog.MonoInstalled)
40        || (FrameworkVersionErrorDialog.MonoInstalled && !FrameworkVersionErrorDialog.MonoCorrectVersionInstalled)) {
41        Application.EnableVisualStyles();
42        Application.SetCompatibleTextRenderingDefault(false);
43        Application.Run(new FrameworkVersionErrorDialog());
44      } else {
45        try {
46          Application.EnableVisualStyles();
47          Application.SetCompatibleTextRenderingDefault(false);
[13389]48
49          string pluginPath = Path.GetFullPath(Application.StartupPath);
50
51          var pluginManager = new PluginManager(pluginPath);
52
53          var splashScreen = new SplashScreen("Loading plugins...", 3000);
54          pluginManager.Initializing += (sender, eventArgs) => splashScreen.UpdateMessage("Loading plugins...");
55          pluginManager.PluginLoaded += (sender, eventArgs) => splashScreen.UpdateMessage("Loaded " + eventArgs.Entity.ToString());
56          pluginManager.ApplicationStarting += (sender, eventArgs) => splashScreen.UpdateMessage("Starting " + eventArgs.Entity.ToString());
57          pluginManager.ApplicationStarted += (sender, eventArgs) => splashScreen.UpdateMessage("Started " + eventArgs.Entity.ToString());
58
59
60          DiscoverPluginsAndRunOptimizerAsync(pluginManager, args);
61
62          Application.Run(splashScreen);
[13338]63        } catch (Exception ex) {
64          MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
65        }
66      }
[2492]67    }
[13389]68
69    private static void DiscoverPluginsAndRunOptimizerAsync(PluginManager pluginManager, string[] args) {
70      // STAThread is necessary for a UI component we are using in the application
71      var t = new Thread(() => {
72        try {
73          pluginManager.DiscoverAndCheckPlugins();
74          var optimizerApp = pluginManager.Applications.FirstOrDefault(app => app.Name == "Optimizer");
75          pluginManager.Run(optimizerApp, CommandLineArgumentHandling.GetArguments(args));
76        } catch (Exception ex) {
77          MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
78        }
79      });
80
81      // cannot use a task because we need to set STA
82      t.SetApartmentState(ApartmentState.STA);
83      t.Start();
84    }
[2492]85  }
86}
Note: See TracBrowser for help on using the repository browser.