From c9bb075910da983cd3992a39026d3d459580824a Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Sat, 23 Mar 2024 13:49:24 +0100 Subject: [PATCH] App: Do not use FileSystemWatcher any more --- Elwig/App.xaml.cs | 22 +++++++++++++++------- Elwig/Windows/ContextWindow.cs | 1 - 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Elwig/App.xaml.cs b/Elwig/App.xaml.cs index 21d5e46..6817f69 100644 --- a/Elwig/App.xaml.cs +++ b/Elwig/App.xaml.cs @@ -60,10 +60,9 @@ namespace Elwig { public static ClientParameters Client { get; set; } public static Dispatcher MainDispatcher { get; private set; } - private readonly FileSystemWatcher Watcher = new(Path.GetDirectoryName(Config.DatabaseFile) ?? "C:\\") { - NotifyFilter = NotifyFilters.LastWrite, - Filter = "*.sqlite3", - }; + private DateTime LastChanged; + private static DateTime CurrentLastWrite => File.GetLastWriteTime(Config.DatabaseFile); + private readonly DispatcherTimer ContextTimer = new() { Interval = TimeSpan.FromSeconds(2) }; public App() : base() { System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); @@ -73,11 +72,16 @@ namespace Elwig { Scales = []; CurrentApp = this; OverrideCulture(); - Watcher.Changed += new FileSystemEventHandler(OnChanged); - Watcher.EnableRaisingEvents = true; + + ContextTimer.Tick += (object? sender, EventArgs evt) => { + if (CurrentLastWrite > LastChanged) { + LastChanged = CurrentLastWrite; + OnContextChanged(); + } + }; } - private void OnChanged(object source, FileSystemEventArgs e) { + private static void OnContextChanged() { MainDispatcher.BeginInvoke(async () => await HintContextChange()); } @@ -112,6 +116,9 @@ namespace Elwig { return; } + LastChanged = CurrentLastWrite; + ContextTimer.Start(); + Dictionary branches = []; using (var ctx = new AppDbContext()) { branches = ctx.Branches.ToDictionary(b => b.Name.ToLower(), b => (b.ZwstId, b.Name, b.PostalDest?.AtPlz?.Plz, b.PostalDest?.AtPlz?.Ort.Name, b.Address, b.PhoneNr, b.FaxNr, b.MobileNr)); @@ -193,6 +200,7 @@ namespace Elwig { } public static async Task HintContextChange() { + CurrentApp.LastChanged = CurrentLastWrite; foreach (Window w in CurrentApp.Windows) { if (w is not ContextWindow c) continue; await c.HintContextChange(); diff --git a/Elwig/Windows/ContextWindow.cs b/Elwig/Windows/ContextWindow.cs index f58ff4a..e16b919 100644 --- a/Elwig/Windows/ContextWindow.cs +++ b/Elwig/Windows/ContextWindow.cs @@ -1,5 +1,4 @@ using Elwig.Helpers; -using System; using System.Threading.Tasks; using System.Windows;