Skip to content

Commit 5b2d379

Browse files
enhance logging and restart logic
1 parent 197706b commit 5b2d379

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

FileSyncAppWin/MainForm.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ public MainForm(string[] args)
99
{
1010
InitializeComponent();
1111
Args = args;
12-
this.FormClosing += (s, e) => { FileSyncApp.Program.keepRunning = false; consoleThread?.Join(10_000); };
12+
this.FormClosing += (s, e) => {
13+
NewLogOutput($"FormClosing, Reason {e.CloseReason}");
14+
FileSyncApp.Program.keepRunning = false;
15+
Program.autoRestart = false;
16+
consoleThread?.Join(10_000);
17+
NewLogOutput($"FormClosing, Reason {e.CloseReason}, consoleThread joined");
18+
};
1319

1420
FileSyncApp.Program.JobsReady += (s, e) =>
1521
{

FileSyncAppWin/Program.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.Logging;
2+
using System.Diagnostics;
23
using System.IO.Pipes;
34

45
namespace FileSyncAppWin
@@ -7,17 +8,19 @@ internal static class Program
78
{
89

910
private static Mutex mutex = new Mutex(true, "PraewemaFileSyncAppWin");
10-
private static volatile bool autoRestart = true;
11+
internal static volatile bool autoRestart = true;
1112
private static ILogger logger;
1213
private static Form mainForm;
1314
private const string PipeName = "PraewemaFileSyncAppWinPipe";
15+
private static string[] _args;
1416

1517
/// <summary>
1618
/// The main entry point for the application.
1719
/// </summary>
1820
[STAThread]
1921
static void Main(string[] args)
2022
{
23+
_args = args;
2124
FileSyncApp.Program.ConfigureLogger(args.FirstOrDefault());
2225
logger = FileSyncApp.Program.LoggerFactory.CreateLogger("FileSyncAppWin");
2326
if (args.Contains("noautorestart"))
@@ -147,13 +150,17 @@ private static void RestartApplication()
147150
if (autoRestart)
148151
{
149152
// Get the path of the current application
150-
string assemblyPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
153+
string assemblyPath = Environment.ProcessPath;
151154
// Restart the application using the path
152155
logger?.LogInformation("RestartApplication, starting new process {A}", assemblyPath);
153-
System.Diagnostics.Process.Start(assemblyPath);
156+
ProcessStartInfo processStartInfo = new ProcessStartInfo(assemblyPath);
157+
processStartInfo.Arguments = string.Join(' ',_args);
158+
processStartInfo.WorkingDirectory = Path.GetDirectoryName(assemblyPath);
159+
Process.Start(assemblyPath);
154160

155161
// Exit the current instance of the application
156-
Environment.Exit(0);
162+
163+
Environment.Exit(-1);
157164
}
158165
else
159166
{

0 commit comments

Comments
 (0)