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
292 lines (238 loc) · 11.2 KB
/
Copy pathApp.xaml.cs
File metadata and controls
292 lines (238 loc) · 11.2 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using log4net;
using NETworkManager.Localization;
using NETworkManager.Localization.Resources;
using NETworkManager.Profiles;
using NETworkManager.Settings;
using NETworkManager.Utilities;
namespace NETworkManager;
/*
* Class: App
* 1) Get command line args
* 2) Detect current configuration
* 3) Get assembly info
* 4) Load system-wide policies
* 5) Load local settings
* 6) Load settings
* 7) Load localization / language
*
* Class: MainWindow
* 8) Load appearance
* 9) Load profiles
*/
public partial class App
{
// Single instance identifier
private const string Guid = "6A3F34B2-161F-4F70-A8BC-A19C40F79CFB";
private static readonly ILog Log = LogManager.GetLogger(typeof(App));
private DispatcherTimer _dispatcherTimer;
private Mutex _mutex;
private bool _singleInstanceClose;
public App()
{
ShutdownMode = ShutdownMode.OnMainWindowClose;
}
private void Application_Startup(object sender, StartupEventArgs e)
{
var startLog = $@"
_ _ _____ _____ _ __ __
| \ | | ____|_ _|_ _____ _ __| | _| \/ | __ _ _ __ __ _ __ _ ___ _ __
| \| | _| | | \ \ /\ / / _ \| '__| |/ / |\/| |/ _` | '_ \ / _` |/ _` |/ _ \ '__|
| |\ | |___ | | \ V V / (_) | | | <| | | | (_| | | | | (_| | (_| | __/ |
|_| \_|_____| |_| \_/\_/ \___/|_| |_|\_\_| |_|\__,_|_| |_|\__,_|\__, |\___|_|
|___/
by BornToBeRoot
GitHub.com/BornToBeRoot/NETworkManager
Version: {AssemblyManager.Current.Version}
";
Log.Info(startLog);
// Catch unhandled exception globally
AppDomain.CurrentDomain.UnhandledException += (_, args) =>
{
Log.Fatal("Unhandled exception occured!");
Log.Fatal($"Exception raised by: {args.ExceptionObject}");
};
// Wait until the previous instance has been closed (restart from ui)
if (CommandLineManager.Current.RestartPid != -1)
{
Log.Info(
$"Waiting for another NETworkManager process with Pid {CommandLineManager.Current.RestartPid} to exit...");
var processList = Process.GetProcesses();
var process = processList.FirstOrDefault(x => x.Id == CommandLineManager.Current.RestartPid);
process?.WaitForExit();
Log.Info($"NETworkManager process with Pid {CommandLineManager.Current.RestartPid} has been exited.");
}
// Load system-wide policies
PolicyManager.Load();
// Load (or initialize) local settings
LocalSettingsManager.Load();
// Load (or initialize) settings
if (CommandLineManager.Current.ResetSettings)
SettingsManager.Initialize();
else
SettingsManager.Load();
// Upgrade settings if necessary
var settingsVersion = Version.Parse(SettingsManager.Current.Version);
if (settingsVersion < AssemblyManager.Current.Version)
SettingsManager.Upgrade(settingsVersion, AssemblyManager.Current.Version);
else
Log.Info($"Application settings are already on version {AssemblyManager.Current.Version}.");
// Initialize localization
var localizationManager = LocalizationManager.GetInstance(SettingsManager.Current.Localization_CultureCode);
Strings.Culture = localizationManager.Culture;
Log.Info(
$"Application localization culture has been set to {localizationManager.Current.Code} (Settings value is \"{SettingsManager.Current.Localization_CultureCode}\").");
// Show help window
if (CommandLineManager.Current.Help)
{
Log.Info("Set StartupUri to CommandLineWindow.xaml...");
StartupUri = new Uri("CommandLineWindow.xaml", UriKind.Relative);
return;
}
// Create mutex (to detect single instance)
Log.Info($"Try to acquire mutex with GUID {Guid} for single instance detection...");
_mutex = new Mutex(true, "{" + Guid + "}");
var mutexIsAcquired = _mutex.WaitOne(TimeSpan.Zero, true);
Log.Info($"Mutex value for {Guid} is {mutexIsAcquired}");
// Release mutex
if (mutexIsAcquired)
_mutex.ReleaseMutex();
// If another instance is running, bring it to the foreground
if (!mutexIsAcquired && !SettingsManager.Current.Window_MultipleInstances)
{
// Bring the already running application into the foreground
Log.Info(
"Another NETworkManager process is already running. Trying to bring the window to the foreground...");
SingleInstance.PostMessage(SingleInstance.HWND_BROADCAST, SingleInstance.WM_SHOWME, IntPtr.Zero,
IntPtr.Zero);
// Close the application
_singleInstanceClose = true;
Shutdown();
return;
}
// Setup background job
if (SettingsManager.Current.General_BackgroundJobInterval != 0)
{
Log.Info(
$"Setup background job with interval {SettingsManager.Current.General_BackgroundJobInterval} minute(s)...");
_dispatcherTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMinutes(SettingsManager.Current.General_BackgroundJobInterval)
};
_dispatcherTimer.Tick += DispatcherTimer_Tick;
_dispatcherTimer.Start();
}
else
{
Log.Info("Background job is disabled.");
}
// Setup ThreadPool for the application
ThreadPool.GetMaxThreads(out var workerThreadsMax, out var completionPortThreadsMax);
ThreadPool.GetMinThreads(out var workerThreadsMin, out var completionPortThreadsMin);
var workerThreadsMinNew = workerThreadsMin + SettingsManager.Current.General_ThreadPoolAdditionalMinThreads;
var completionPortThreadsMinNew = completionPortThreadsMin +
SettingsManager.Current.General_ThreadPoolAdditionalMinThreads;
if (workerThreadsMinNew > workerThreadsMax)
workerThreadsMinNew = workerThreadsMax;
if (completionPortThreadsMinNew > completionPortThreadsMax)
completionPortThreadsMinNew = completionPortThreadsMax;
if (ThreadPool.SetMinThreads(workerThreadsMinNew, completionPortThreadsMinNew))
Log.Info(
$"ThreadPool min threads set to: workerThreads: {workerThreadsMinNew}, completionPortThreads: {completionPortThreadsMinNew}");
else
Log.Warn(
$"ThreadPool min threads could not be set to workerThreads: {workerThreadsMinNew}, completionPortThreads: {completionPortThreadsMinNew}");
// Show splash screen
if (SettingsManager.Current.SplashScreen_Enabled)
{
Log.Info("Show SplashScreen while application is loading...");
new SplashScreen(@"SplashScreen.png").Show(true, true);
}
// Show main window
Log.Info("Set StartupUri to MainWindow.xaml...");
StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
}
/// <summary>
/// Handles the tick event of the dispatcher timer to trigger a background job and save data.
/// </summary>
/// <param name="sender">The source of the event, typically the dispatcher timer instance.</param>
/// <param name="e">The event data associated with the timer tick.</param>
private void DispatcherTimer_Tick(object sender, EventArgs e)
{
Log.Info("Run background job...");
Save();
}
/// <summary>
/// Handles the session ending event by canceling the session termination and initiating application shutdown.
/// </summary>
/// <remarks>This method overrides the default session ending behavior to prevent the session from ending
/// automatically. Instead, it cancels the session termination and shuts down the application gracefully. Use this
/// override to implement custom shutdown logic when the user logs off or shuts down the system.</remarks>
/// <param name="e">The event data for the session ending event. Provides information about the session ending request and allows
/// cancellation of the event.</param>
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
{
base.OnSessionEnding(e);
e.Cancel = true;
Shutdown();
}
/// <summary>
/// Handles the application exit event to perform cleanup operations such as stopping background tasks and saving
/// settings.
/// </summary>
/// <remarks>This method is intended to be called when the application is shutting down. It ensures that
/// any running background jobs are stopped and application settings are saved, unless the application is closed due
/// to a single instance or help command scenario.</remarks>
/// <param name="sender">The source of the event, typically the application instance.</param>
/// <param name="e">The event data associated with the application exit event.</param>
private void Application_Exit(object sender, ExitEventArgs e)
{
Log.Info("Exiting NETworkManager...");
// Save settings, when the application is normally closed
if (_singleInstanceClose || CommandLineManager.Current.Help)
return;
Log.Info("Stop background job, if it exists...");
_dispatcherTimer?.Stop();
Save();
Log.Info("Bye!");
}
/// <summary>
/// Saves application settings and profile data if changes have been detected.
/// </summary>
/// <remarks>This method saves settings only if they have been modified. Profile data is saved if a
/// profile file is loaded, its data is available, and changes have been made. If no profile file is loaded or the
/// file is encrypted and not unlocked, profile data will not be saved and a warning is logged.</remarks>
private void Save()
{
// Save local settings if they have changed
if (LocalSettingsManager.Current.SettingsChanged)
{
Log.Info("Save local settings...");
LocalSettingsManager.Save();
}
// Save settings if they have changed
if (SettingsManager.Current.SettingsChanged)
{
Log.Info("Save application settings...");
SettingsManager.Save();
}
// Save profiles if they have changed
if (ProfileManager.LoadedProfileFile != null && ProfileManager.LoadedProfileFileData != null)
{
if (ProfileManager.LoadedProfileFileData.ProfilesChanged)
{
Log.Info($"Save current profile file \"{ProfileManager.LoadedProfileFile.Name}\"...");
ProfileManager.Save();
}
}
else
{
Log.Warn("Cannot save profiles because no profile file is loaded or the profile file is encrypted and not yet unlocked.");
}
}
}