Skip to content

Commit 5dc1891

Browse files
committed
fix #34, #33 improved
1 parent eafc43b commit 5dc1891

5 files changed

Lines changed: 83 additions & 44 deletions

File tree

Source/NETworkManager/App.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Application x:Class="NETworkManager.App"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
ShutdownMode="OnMainWindowClose" StartupUri="MainWindow.xaml">
4+
Startup="Application_Startup" Exit="Application_Exit">
55
<Application.Resources>
66
<ResourceDictionary>
77
<ResourceDictionary.MergedDictionaries>

Source/NETworkManager/App.xaml.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using NETworkManager.Models.Settings;
2+
using System;
3+
using System.Diagnostics;
4+
using System.Linq;
25
using System.Windows;
36

47
namespace NETworkManager
@@ -10,7 +13,60 @@ public partial class App : Application
1013
{
1114
public App()
1215
{
13-
ShutdownMode = ShutdownMode.OnLastWindowClose;
16+
ShutdownMode = ShutdownMode.OnLastWindowClose;
17+
}
18+
19+
private void Application_Startup(object sender, StartupEventArgs e)
20+
{
21+
// Parse the command line arguments and store them in the current configuration
22+
CommandLineManager.Parse();
23+
24+
// If we have restart our application... wait until it has finished
25+
if (CommandLineManager.Current.RestartPid != 0)
26+
{
27+
Process[] processList = Process.GetProcesses();
28+
29+
Process process = processList.FirstOrDefault(x => x.Id == CommandLineManager.Current.RestartPid);
30+
31+
if (process != null)
32+
process.WaitForExit();
33+
}
34+
35+
// Detect the current configuration
36+
ConfigurationManager.Detect();
37+
38+
// Load settings
39+
SettingsManager.Load();
40+
TemplateManager.LoadNetworkInterfaceConfigTemplates();
41+
TemplateManager.LoadWakeOnLANTemplates();
42+
43+
StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
44+
}
45+
46+
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
47+
{
48+
base.OnSessionEnding(e);
49+
50+
e.Cancel = true;
51+
52+
Shutdown();
53+
}
54+
55+
private void Application_Exit(object sender, ExitEventArgs e)
56+
{
57+
if (!ImportExportManager.ForceRestart)
58+
{
59+
// Save templates
60+
if (TemplateManager.NetworkInterfaceConfigTemplatesChanged)
61+
TemplateManager.SaveNetworkInterfaceConfigTemplates();
62+
63+
if (TemplateManager.WakeOnLANTemplatesChanged)
64+
TemplateManager.SaveWakeOnLANTemplates();
65+
66+
// Save settings
67+
if (SettingsManager.Current.SettingsChanged)
68+
SettingsManager.Save();
69+
}
1470
}
1571
}
1672
}

Source/NETworkManager/MainWindow.xaml.cs

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ private void OnPropertyChanged(string propertyName)
4242
private bool _isLoading = true;
4343

4444
private bool _isInTray;
45-
private bool _restartApplication;
4645
private bool _closeApplication;
4746

4847
// Indicates a restart message, when settings changed
@@ -220,15 +219,6 @@ public MainWindow()
220219
InitializeComponent();
221220
DataContext = this;
222221

223-
// Parse the command line arguments and store them in the current configuration
224-
CommandLineManager.Parse();
225-
226-
// Detect the current configuration
227-
ConfigurationManager.Detect();
228-
229-
// Load settings
230-
SettingsManager.Load();
231-
232222
// Get assembly informations
233223
AssemblyManager.Load();
234224
Version = AssemblyManager.Current.AssemblyVersion.ToString();
@@ -269,10 +259,6 @@ public MainWindow()
269259
// Load settings
270260
ApplicationView_Expand = SettingsManager.Current.ApplicationView_Expand;
271261

272-
// Load templates
273-
TemplateManager.LoadNetworkInterfaceConfigTemplates();
274-
TemplateManager.LoadWakeOnLANTemplates();
275-
276262
_isLoading = false;
277263
}
278264

@@ -295,7 +281,7 @@ private void LoadApplicationList()
295281
private async void MetroWindowMain_Closing(object sender, CancelEventArgs e)
296282
{
297283
// Hide the application to tray
298-
if (!_closeApplication && !_restartApplication && SettingsManager.Current.Window_MinimizeInsteadOfTerminating)
284+
if (!_closeApplication && SettingsManager.Current.Window_MinimizeInsteadOfTerminating)
299285
{
300286
e.Cancel = true;
301287

@@ -305,7 +291,7 @@ private async void MetroWindowMain_Closing(object sender, CancelEventArgs e)
305291
}
306292

307293
// Confirm close
308-
if (!_closeApplication && !_restartApplication && SettingsManager.Current.Window_ConfirmClose)
294+
if (!_closeApplication && SettingsManager.Current.Window_ConfirmClose)
309295
{
310296
e.Cancel = true;
311297

@@ -324,9 +310,6 @@ private async void MetroWindowMain_Closing(object sender, CancelEventArgs e)
324310
return;
325311
}
326312

327-
if (!_restartApplication && !ImportExportManager.ForceRestart)
328-
SaveTemplatesAndSettings();
329-
330313
// Unregister HotKeys
331314
if (RegisteredHotKeys.Count > 0)
332315
UnregisterHotKeys();
@@ -335,20 +318,6 @@ private async void MetroWindowMain_Closing(object sender, CancelEventArgs e)
335318
if (notifyIcon != null)
336319
notifyIcon.Dispose();
337320
}
338-
339-
private void SaveTemplatesAndSettings()
340-
{
341-
// Save templates
342-
if (TemplateManager.NetworkInterfaceConfigTemplatesChanged)
343-
TemplateManager.SaveNetworkInterfaceConfigTemplates();
344-
345-
if (TemplateManager.WakeOnLANTemplatesChanged)
346-
TemplateManager.SaveWakeOnLANTemplates();
347-
348-
// Save settings
349-
if (SettingsManager.Current.SettingsChanged)
350-
SettingsManager.Save();
351-
}
352321
#endregion
353322

354323
#region Application Views
@@ -740,11 +709,16 @@ private void CloseApplicationAction()
740709

741710
private void RestartApplication()
742711
{
743-
SaveTemplatesAndSettings();
744-
745-
Process.Start(ConfigurationManager.Current.ApplicationFullName);
712+
new Process
713+
{
714+
StartInfo =
715+
{
716+
FileName = ConfigurationManager.Current.ApplicationFullName,
717+
Arguments = string.Format("--restart-pid:{0}", Process.GetCurrentProcess().Id)
718+
}
719+
}.Start();
746720

747-
_restartApplication = true;
721+
_closeApplication = true;
748722
Close();
749723
}
750724

Source/NETworkManager/Models/Settings/CommandLineInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public class CommandLineInfo
44
{
55
public bool Autostart { get; set; }
66
public bool ResetSettings { get; set; }
7+
public int RestartPid { get; set; }
78

89
public CommandLineInfo()
910
{

Source/NETworkManager/Models/Settings/CommandLineManager.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public static class CommandLineManager
99
public const string ParameterIdentifier = "--";
1010
public const string ParameterAutostart = "autostart";
1111
public const string ParameterResetSettings = "reset-settings";
12+
public const string ParameterRestartPid = "restart-pid:";
1213

1314
public static CommandLineInfo Current { get; set; }
1415

@@ -30,15 +31,22 @@ public static void Parse()
3031
{
3132
if (parameter.StartsWith(ParameterIdentifier))
3233
{
34+
string param = parameter.TrimStart(trimChars);
35+
3336
// Autostart
34-
if (parameter.TrimStart(trimChars).Equals(ParameterAutostart, StringComparison.InvariantCultureIgnoreCase))
37+
if (param.Equals(ParameterAutostart, StringComparison.InvariantCultureIgnoreCase))
38+
{
3539
Current.Autostart = true;
36-
else if (parameter.TrimStart(trimChars).Equals(ParameterResetSettings, StringComparison.InvariantCultureIgnoreCase))
40+
} // Reset Settings
41+
else if (param.Equals(ParameterResetSettings, StringComparison.InvariantCultureIgnoreCase))
42+
{
3743
Current.ResetSettings = true;
38-
else
44+
} // Restart
45+
else if (param.StartsWith(ParameterRestartPid, StringComparison.InvariantCultureIgnoreCase))
3946
{
40-
MetroMessageBox msg = new MetroMessageBox();
41-
msg.ShowDialog();
47+
int.TryParse(param.Split(':')[1], out int restartPid);
48+
49+
Current.RestartPid = restartPid;
4250
}
4351
}
4452
}

0 commit comments

Comments
 (0)