From d28909bd98e63a115d5166546b5a0bbeb383d343 Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch> Date: Thu, 8 Feb 2018 18:16:51 +0100 Subject: [PATCH] Use FileSystemWatcher to monitor changes to the status file. Refers to #23 --- ATxTray/AutoTxTray.cs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/ATxTray/AutoTxTray.cs b/ATxTray/AutoTxTray.cs index 5e4ee44..62cbec9 100644 --- a/ATxTray/AutoTxTray.cs +++ b/ATxTray/AutoTxTray.cs @@ -28,10 +28,10 @@ namespace ATxTray private static string _statusFile; private static string _submitPath; - private static DateTime _statusAge; private static ServiceConfig _config; private static ServiceStatus _status; + private static bool _statusFileChanged = false; private static bool _statusChanged = false; private static bool _svcRunning = false; private static bool _svcSuspended = true; @@ -120,6 +120,14 @@ namespace ATxTray AppTimer.Enabled = true; Log.Trace("Enabled timer."); + var fsw = new FileSystemWatcher { + Path = baseDir, + NotifyFilter = NotifyFilters.LastWrite, + Filter = "status.xml", + }; + fsw.Changed += StatusUpdated; + fsw.EnableRaisingEvents = true; + Log.Info("AutoTxTray initialization completed."); } @@ -219,6 +227,10 @@ namespace ATxTray UpdateHoverText($"AutoTx [svc={serviceRunning}] [hb={heartBeat}] [tx={txProgress}]"); } + private static void StatusUpdated(object sender, FileSystemEventArgs e) { + _statusFileChanged = true; + } + private void MiExitClick(object sender, EventArgs e) { AutoTxTrayExit(); } @@ -343,13 +355,12 @@ namespace ATxTray /// Read (or re-read) the service status file if it has changed since last time. /// </summary> private static void ReadStatus() { - var age = new FileInfo(_statusFile).LastWriteTime; - if (age == _statusAge) + if (!_statusFileChanged) return; - Log.Trace("Status file was updated, trying to re-read..."); - _statusAge = age; + Log.Debug("Status file was updated, trying to re-read..."); _status = ServiceStatus.Deserialize(_statusFile, _config); + _statusFileChanged = false; } /// <summary> -- GitLab