2017-07-05 17:21:52 +02:00
|
|
|
|
/*
|
2019-01-09 11:25:21 +01:00
|
|
|
|
* Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET)
|
2017-07-28 14:52:15 +02:00
|
|
|
|
*
|
2017-07-05 17:21:52 +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;
|
2018-11-22 14:36:20 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2017-07-05 17:21:52 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
2018-01-18 08:16:20 +01:00
|
|
|
|
namespace SafeExamBrowser.Runtime
|
2017-07-05 17:21:52 +02:00
|
|
|
|
{
|
|
|
|
|
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");
|
2017-07-13 08:51:00 +02:00
|
|
|
|
private CompositionRoot instances = new CompositionRoot();
|
2017-07-10 15:47:12 +02:00
|
|
|
|
|
2017-07-05 17:21:52 +02:00
|
|
|
|
[STAThread]
|
|
|
|
|
public static void Main()
|
|
|
|
|
{
|
2017-07-06 10:56:03 +02:00
|
|
|
|
try
|
2017-07-05 17:21:52 +02:00
|
|
|
|
{
|
2017-07-07 15:46:32 +02:00
|
|
|
|
StartApplication();
|
2017-07-05 17:21:52 +02:00
|
|
|
|
}
|
2017-07-06 10:56:03 +02:00
|
|
|
|
catch (Exception e)
|
2017-07-05 17:21:52 +02:00
|
|
|
|
{
|
2017-07-19 14:43:54 +02:00
|
|
|
|
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();
|
2017-07-05 17:21:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-07 15:46:32 +02:00
|
|
|
|
private static void StartApplication()
|
2017-07-05 17:21:52 +02:00
|
|
|
|
{
|
2017-07-10 15:47:12 +02:00
|
|
|
|
if (NoInstanceRunning())
|
|
|
|
|
{
|
|
|
|
|
new App().Run();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-07-19 14:43:54 +02:00
|
|
|
|
MessageBox.Show("You can only run one instance of SEB at a time.", "Startup Not Allowed", MessageBoxButton.OK, MessageBoxImage.Information);
|
2017-07-10 15:47:12 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2017-07-10 15:47:12 +02:00
|
|
|
|
private static bool NoInstanceRunning()
|
|
|
|
|
{
|
2017-07-28 14:52:15 +02:00
|
|
|
|
return Mutex.WaitOne(TimeSpan.Zero, true);
|
2017-07-10 15:47:12 +02:00
|
|
|
|
}
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
2017-07-10 15:47:12 +02:00
|
|
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnStartup(e);
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2018-01-30 14:41:36 +01:00
|
|
|
|
ShutdownMode = ShutdownMode.OnExplicitShutdown;
|
|
|
|
|
|
2018-02-21 14:01:21 +01:00
|
|
|
|
instances.BuildObjectGraph(Shutdown);
|
2018-01-23 15:33:54 +01:00
|
|
|
|
instances.LogStartupInformation();
|
2017-07-17 08:28:18 +02:00
|
|
|
|
|
2018-11-22 14:36:20 +01:00
|
|
|
|
Task.Run(new Action(TryStart));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TryStart()
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var success = instances.RuntimeController.TryStart();
|
2017-07-17 08:28:18 +02:00
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
if (!success)
|
2017-07-17 08:28:18 +02:00
|
|
|
|
{
|
|
|
|
|
Shutdown();
|
|
|
|
|
}
|
2017-07-19 14:43:54 +02:00
|
|
|
|
}
|
2017-07-17 08:28:18 +02:00
|
|
|
|
|
2018-02-21 14:01:21 +01:00
|
|
|
|
public new void Shutdown()
|
2018-01-19 14:04:12 +01:00
|
|
|
|
{
|
2018-11-22 14:36:20 +01:00
|
|
|
|
Task.Run(new Action(ShutdownInternal));
|
|
|
|
|
}
|
2018-06-21 07:56:25 +02:00
|
|
|
|
|
2018-11-22 14:36:20 +01:00
|
|
|
|
private void ShutdownInternal()
|
|
|
|
|
{
|
|
|
|
|
instances.RuntimeController.Terminate();
|
|
|
|
|
instances.LogShutdownInformation();
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
2018-11-22 14:36:20 +01:00
|
|
|
|
Dispatcher.Invoke(base.Shutdown);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
}
|
2017-07-05 17:21:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|