forked from BornToBeRoot/NETworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
157 lines (127 loc) · 4.92 KB
/
App.xaml.cs
File metadata and controls
157 lines (127 loc) · 4.92 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using NETworkManager.Utilities;
using NETworkManager.Settings;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using NETworkManager.Profiles;
using NETworkManager.Localization;
namespace NETworkManager
{
/*
* Class: App
* 1) Get command line args
* 2) Detect current configuration
* 3) Get assembly info
* 4) Load settings
* 5) Load localization / language
*
* Class: MainWindow
* 6) Load appearance
* 7) Load profiles
*/
public partial class App
{
// Single instance identifier
private const string GUID = "6A3F34B2-161F-4F70-A8BC-A19C40F79CFB";
private Mutex _mutex;
private DispatcherTimer _dispatcherTimer;
private bool _singleInstanceClose;
public App()
{
ShutdownMode = ShutdownMode.OnMainWindowClose;
}
private void Application_Startup(object sender, StartupEventArgs e)
{
// If we have restart our application... wait until it has finished
if (CommandLineManager.Current.RestartPid != -1)
{
var processList = Process.GetProcesses();
var process = processList.FirstOrDefault(x => x.Id == CommandLineManager.Current.RestartPid);
process?.WaitForExit();
}
// Update integrated settings %LocalAppData%\NETworkManager\NETworkManager_GUID (custom settings path)
if (LocalSettingsManager.UpgradeRequired)
{
LocalSettingsManager.Upgrade();
LocalSettingsManager.UpgradeRequired = false;
}
// Load settings
try
{
SettingsManager.Load();
}
catch (InvalidOperationException)
{
SettingsManager.InitDefault();
ConfigurationManager.Current.ShowSettingsResetNoteOnStartup = true;
}
// Init the location with the culture code...
Localization.Resources.Strings.Culture = LocalizationManager.GetInstance(SettingsManager.Current.Localization_CultureCode).Culture;
if (CommandLineManager.Current.Help)
{
StartupUri = new Uri("CommandLineWindow.xaml", UriKind.Relative);
return;
}
// Create mutex
_mutex = new Mutex(true, "{" + GUID + "}");
var mutexIsAcquired = _mutex.WaitOne(TimeSpan.Zero, true);
// Release mutex
if (mutexIsAcquired)
_mutex.ReleaseMutex();
if (SettingsManager.Current.Window_MultipleInstances || mutexIsAcquired)
{
if (SettingsManager.Current.General_BackgroundJobInterval != 0)
{
_dispatcherTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMinutes(SettingsManager.Current.General_BackgroundJobInterval)
};
_dispatcherTimer.Tick += DispatcherTimer_Tick;
_dispatcherTimer.Start();
}
// Show splash screen
if (SettingsManager.Current.SplashScreen_Enabled)
new SplashScreen(@"SplashScreen.png").Show(true, true);
// Show main window
StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
}
else
{
// Bring the already running application into the foreground
SingleInstance.PostMessage((IntPtr)SingleInstance.HWND_BROADCAST, SingleInstance.WM_SHOWME, IntPtr.Zero, IntPtr.Zero);
_singleInstanceClose = true;
Shutdown();
}
}
private void DispatcherTimer_Tick(object sender, EventArgs e)
{
Save();
}
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
{
base.OnSessionEnding(e);
e.Cancel = true;
Shutdown();
}
private void Application_Exit(object sender, ExitEventArgs e)
{
// Save settings, when the application is normally closed
if (_singleInstanceClose || ConfigurationManager.Current.ForceRestart || CommandLineManager.Current.Help)
return;
_dispatcherTimer?.Stop();
Save();
}
private void Save()
{
// Save local settings (custom settings path in AppData/Local)
LocalSettingsManager.Save();
if (SettingsManager.Current.SettingsChanged) // This will also create the "Settings" folder, if it does not exist
SettingsManager.Save();
if (ProfileManager.ProfilesChanged)
ProfileManager.Save();
}
}
}