forked from NexerqDev/Verdant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
52 lines (48 loc) · 1.85 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace Verdant
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
#if !DEBUG
public App() : base()
{
this.Dispatcher.UnhandledException += OnDispatcherUnhandledException;
}
// single instance ~ some from https://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-wpf-application
static Mutex siMutex = new Mutex(true, "NexerqDev.Verdant-SingleInstance");
protected override void OnStartup(StartupEventArgs e)
{
if (!siMutex.WaitOne(TimeSpan.Zero, true))
{
MessageBox.Show("Only one instance of Verdant may be running at the same time!", "Error - Verdant", MessageBoxButton.OK, MessageBoxImage.Error);
Application.Current.Shutdown();
siMutex = null;
return;
}
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
if (siMutex != null)
siMutex.ReleaseMutex();
base.OnExit(e);
}
void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show("A fatal error has been thrown in Verdant. Please report this error, alongside what you did in the lead up (in DETAIL) to the GitHub issue tracker - this would be much appreciated. Thank you!\n\n" + e.Exception.ToString());
Application.Current.Shutdown();
e.Handled = true;
}
#endif
}
}