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
178 lines (140 loc) · 5.92 KB
/
Copy pathApp.xaml.cs
File metadata and controls
178 lines (140 loc) · 5.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using NETworkManager.Utilities;
using NETworkManager.Models.Settings;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using NETworkManager.Properties;
namespace NETworkManager
{
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)
{
// Parse the command line arguments and store them in the current configuration
CommandLineManager.Parse();
// If we have restart our application... wait until it has finished
if (CommandLineManager.Current.RestartPid != 0)
{
var processList = Process.GetProcesses();
var process = processList.FirstOrDefault(x => x.Id == CommandLineManager.Current.RestartPid);
process?.WaitForExit();
}
// Detect the current configuration
ConfigurationManager.Detect();
// Get assembly informations
AssemblyManager.Load();
bool profileUpdateRequired = false;
// Load application settings
try
{
// Update integrated settings %LocalAppData%\NETworkManager\NETworkManager_GUID (custom settings path)
if (Settings.Default.UpgradeRequired)
{
Settings.Default.Upgrade();
Settings.Default.UpgradeRequired = false;
}
SettingsManager.Load();
// Update settings (Default --> %AppData%\NETworkManager\Settings)
Version assemblyVersion = AssemblyManager.Current.Version;
Version settingsVersion = new Version(SettingsManager.Current.SettingsVersion);
if (AssemblyManager.Current.Version > settingsVersion)
{
SettingsManager.Update(AssemblyManager.Current.Version, settingsVersion);
}
}
catch (InvalidOperationException)
{
SettingsManager.InitDefault();
profileUpdateRequired = true; // Because we don't know if a profile update is required
ConfigurationManager.Current.ShowSettingsResetNoteOnStartup = true;
}
// Upgrade profile if version has changed or settings have been reset (mthis happens mostly, because the version has changed and values are wrong)
if(profileUpdateRequired)
{
try
{
ProfileManager.Upgrade();
}
catch (Exception ex)
{
MessageBox.Show("Failed to update profiles...\n\n" + ex.Message, "Profile Manager - Update Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
// Load localization (requires settings to be loaded first)
LocalizationManager.Load();
NETworkManager.Resources.Localization.Strings.Culture = LocalizationManager.Culture;
if (CommandLineManager.Current.Help)
{
StartupUri = new Uri("/Views/CommandLineHelpWindow.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();
}
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 || ImportExportManager.ForceRestart || CommandLineManager.Current.Help)
return;
_dispatcherTimer?.Stop();
Save();
}
private void Save()
{
// Save local settings (custom settings path in AppData/Local)
Settings.Default.Save();
if (SettingsManager.Current.SettingsChanged) // This will also create the "Settings" folder, if it does not exist
SettingsManager.Save();
if (ProfileManager.ProfilesChanged)
ProfileManager.Save();
if (CredentialManager.CredentialsChanged)
CredentialManager.Save();
}
}
}