seb-win-refactoring/SafeExamBrowser.Runtime/App.cs

80 lines
1.7 KiB
C#
Raw Normal View History

/*
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
2017-07-28 14:52:15 +02:00
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using System.Threading;
using System.Windows;
namespace SafeExamBrowser.Runtime
{
public class App : Application
{
2018-01-16 08:14:57 +01:00
private static readonly Mutex Mutex = new Mutex(true, "safe_exam_browser_runtime_mutex");
private CompositionRoot instances = new CompositionRoot();
[STAThread]
public static void Main()
{
try
{
StartApplication();
}
catch (Exception e)
{
MessageBox.Show(e.Message + "\n\n" + e.StackTrace, "Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
2017-07-28 14:52:15 +02:00
Mutex.Close();
}
}
private static void StartApplication()
{
if (NoInstanceRunning())
{
new App().Run();
}
else
{
MessageBox.Show("You can only run one instance of SEB at a time.", "Startup Not Allowed", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private static bool NoInstanceRunning()
{
2017-07-28 14:52:15 +02:00
return Mutex.WaitOne(TimeSpan.Zero, true);
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
ShutdownMode = ShutdownMode.OnExplicitShutdown;
instances.BuildObjectGraph();
instances.LogStartupInformation();
var success = instances.RuntimeController.TryStart(instances.StartupOperations);
if (!success)
{
Shutdown();
}
}
protected override void OnExit(ExitEventArgs e)
{
instances.RuntimeController.Terminate();
instances.LogShutdownInformation();
base.OnExit(e);
}
}
}