2017-07-05 17:21:52 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2017 ETH Zürich, Educational Development and Technology (LET)
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
{
|
|
|
|
|
public class App : Application
|
|
|
|
|
{
|
2017-07-07 15:46:32 +02:00
|
|
|
|
private static readonly Mutex mutex = new Mutex(true, "safe_exam_browser_single_instance_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-06 18:18:39 +02:00
|
|
|
|
MessageBox.Show(e.Message + "\n\n" + e.StackTrace, "Fatal Error");
|
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
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("You can only run one instance of SEB at a time.", "Startup Not Allowed");
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2017-07-10 15:47:12 +02:00
|
|
|
|
private static bool NoInstanceRunning()
|
|
|
|
|
{
|
|
|
|
|
return mutex.WaitOne(TimeSpan.Zero, true);
|
|
|
|
|
}
|
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
|
|
|
|
|
2017-07-13 08:51:00 +02:00
|
|
|
|
ShowSplashScreen();
|
2017-07-14 10:28:59 +02:00
|
|
|
|
InitializeApplication();
|
2017-07-10 15:47:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnExit(ExitEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
instances.ShutdownController.FinalizeApplication();
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2017-07-10 15:47:12 +02:00
|
|
|
|
base.OnExit(e);
|
2017-07-06 10:56:03 +02:00
|
|
|
|
}
|
2017-07-05 17:21:52 +02:00
|
|
|
|
|
2017-07-14 10:28:59 +02:00
|
|
|
|
private void InitializeApplication()
|
2017-07-06 10:56:03 +02:00
|
|
|
|
{
|
2017-07-13 08:51:00 +02:00
|
|
|
|
instances.BuildObjectGraph();
|
|
|
|
|
|
2017-07-10 15:47:12 +02:00
|
|
|
|
var success = instances.StartupController.TryInitializeApplication();
|
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
2017-07-13 08:51:00 +02:00
|
|
|
|
MainWindow = instances.Taskbar;
|
|
|
|
|
MainWindow.Show();
|
2017-07-10 15:47:12 +02:00
|
|
|
|
}
|
2017-07-12 15:36:30 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
2017-07-13 08:51:00 +02:00
|
|
|
|
Shutdown();
|
2017-07-12 15:36:30 +02:00
|
|
|
|
}
|
2017-07-10 15:47:12 +02:00
|
|
|
|
|
2017-07-13 08:51:00 +02:00
|
|
|
|
instances.SplashScreen?.Dispatcher.InvokeAsync(instances.SplashScreen.Close);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ShowSplashScreen()
|
|
|
|
|
{
|
2017-07-14 10:28:59 +02:00
|
|
|
|
instances.BuildModulesRequiredBySplashScreen();
|
|
|
|
|
|
2017-07-13 08:51:00 +02:00
|
|
|
|
var splashReadyEvent = new AutoResetEvent(false);
|
|
|
|
|
var splashScreenThread = new Thread(() =>
|
|
|
|
|
{
|
2017-07-14 10:28:59 +02:00
|
|
|
|
instances.SplashScreen = new UserInterface.SplashScreen(instances.Settings, instances.Text);
|
2017-07-13 08:51:00 +02:00
|
|
|
|
instances.SplashScreen.Closed += (o, args) => instances.SplashScreen.Dispatcher.InvokeShutdown();
|
|
|
|
|
instances.SplashScreen.Show();
|
|
|
|
|
|
|
|
|
|
splashReadyEvent.Set();
|
|
|
|
|
|
|
|
|
|
System.Windows.Threading.Dispatcher.Run();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
splashScreenThread.SetApartmentState(ApartmentState.STA);
|
|
|
|
|
splashScreenThread.Name = "Splash Screen Thread";
|
|
|
|
|
splashScreenThread.IsBackground = true;
|
|
|
|
|
splashScreenThread.Start();
|
|
|
|
|
|
|
|
|
|
splashReadyEvent.WaitOne();
|
2017-07-05 17:21:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|