40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace RhinoReminds.Utilities
|
|
{
|
|
public static class ExitWatcher
|
|
{
|
|
|
|
private static int restartDelayMs = 5 * 1000;
|
|
private static string EntryExecutablePath => Assembly.GetEntryAssembly().Location;
|
|
|
|
private static FileSystemWatcher watcher;
|
|
private static bool exitSequenceStarted = false;
|
|
|
|
public static void EnableExitOnModify() {
|
|
Console.WriteLine("[Program/ExitWatcher] Enabling auto-exit on modification.");
|
|
watcher = new FileSystemWatcher(Path.GetDirectoryName(EntryExecutablePath), "*.exe");
|
|
watcher.Changed += (object sender, FileSystemEventArgs e) => doExit();
|
|
|
|
watcher.EnableRaisingEvents = true;
|
|
}
|
|
|
|
private static void doExit() {
|
|
// Don't bother if we're already exiting on a different thread or something else weird
|
|
if (exitSequenceStarted) return;
|
|
|
|
exitSequenceStarted = true;
|
|
watcher.EnableRaisingEvents = false; // We're only interested in the first event raised
|
|
|
|
Console.WriteLine($"[Program/ExitWatcher] Modification detected, waiting {restartDelayMs}ms...");
|
|
Thread.Sleep(restartDelayMs);
|
|
Console.WriteLine($"[Program/ExitWatcher] Wait complete, exiting with code 0");
|
|
Environment.Exit(0);
|
|
}
|
|
}
|
|
}
|