-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
97 lines (83 loc) · 2.1 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace ExchangeRateServer
{
public partial class App : Application
{
internal static bool flag_log;
internal static bool flag_debug;
private static readonly CultureInfo Culture = new("en-US");
private const string UniqueEventName = "{3596C906-E192-47BD-B890-B79591261EDD}";
private EventWaitHandle eventWaitHandle;
public App()
{
ShutdownMode = ShutdownMode.OnExplicitShutdown;
SingleInstanceWatcher();
CultureInfo.DefaultThreadCurrentCulture = Culture;
}
private void SingleInstanceWatcher()
{
try
{
eventWaitHandle = EventWaitHandle.OpenExisting(UniqueEventName);
_ = eventWaitHandle.Set();
Shutdown();
}
catch (WaitHandleCannotBeOpenedException)
{
eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, UniqueEventName);
}
new Task(() =>
{
while (eventWaitHandle.WaitOne())
{
_ = Current.Dispatcher.BeginInvoke((Action)(() =>
{
if (!Current.MainWindow.Equals(null))
{
var mw = Current.MainWindow;
if (mw.WindowState == WindowState.Minimized || mw.Visibility != Visibility.Visible)
{
mw.Show();
mw.WindowState = WindowState.Normal;
}
_ = mw.Activate();
mw.Topmost = true;
mw.Topmost = false;
_ = mw.Focus();
}
}));
}
})
.Start();
}
protected override void OnStartup(StartupEventArgs e)
{
ShutdownMode = ShutdownMode.OnMainWindowClose;
Current.DispatcherUnhandledException += (x, ex) =>
{
if (!Directory.Exists("log"))
{
_ = Directory.CreateDirectory("log");
};
File.AppendAllText(@"log\crash.txt", "Unknown Application Wide Exception: " + ex.Exception.Message + "\n" + ex.Exception.StackTrace);
};
for (var i = 0; i != e.Args.Length; ++i)
{
if (e.Args[i] == "/log")
{
flag_log = true;
}
if (e.Args[i] == "/debug")
{
flag_debug = true;
}
}
base.OnStartup(e);
}
}
}