[#43] App: Use FileSystemWatcher to renew contexts on demand
This commit is contained in:
		| @@ -2,25 +2,14 @@ using Elwig.Helpers; | ||||
| using System; | ||||
| using System.Threading.Tasks; | ||||
| using System.Windows; | ||||
| using System.Windows.Threading; | ||||
|  | ||||
| namespace Elwig.Windows { | ||||
|     public abstract class ContextWindow : Window { | ||||
|  | ||||
|         public static readonly int RenewSec = 10; | ||||
|  | ||||
|         protected AppDbContext Context { get; private set; } | ||||
|         protected bool LockContext { get; set; } = false; | ||||
|  | ||||
|         private readonly DispatcherTimer _timer; | ||||
|         private bool _renewPending = false; | ||||
|  | ||||
|         public ContextWindow() : base() { | ||||
|             _timer = new DispatcherTimer(); | ||||
|             _timer.Tick += new EventHandler(OnShouldRenewContext); | ||||
|             _timer.Interval = new TimeSpan(0, 0, RenewSec); | ||||
|             _timer.Start(); | ||||
|             Context = new(); | ||||
|             Loaded += OnLoaded; | ||||
|         } | ||||
|  | ||||
| @@ -30,22 +19,18 @@ namespace Elwig.Windows { | ||||
|             await RenewContext(); | ||||
|         } | ||||
|  | ||||
|         private async void OnShouldRenewContext(object? sender, EventArgs? evt) { | ||||
|             if (!Context.HasBackendChanged) return; | ||||
|             await HintContextChange(); | ||||
|         } | ||||
|  | ||||
|         protected async void OnLoaded(object? sender, RoutedEventArgs? evt) { | ||||
|             await OnRenewContext(); | ||||
|             using var ctx = new AppDbContext(); | ||||
|             await OnRenewContext(ctx); | ||||
|         } | ||||
|  | ||||
|         protected async Task RenewContext() { | ||||
|             if (!_renewPending) return; | ||||
|             Context = new(); | ||||
|             await OnRenewContext(); | ||||
|             using var ctx = new AppDbContext(); | ||||
|             await OnRenewContext(ctx); | ||||
|             _renewPending = false; | ||||
|         } | ||||
|  | ||||
|         abstract protected Task OnRenewContext(); | ||||
|         abstract protected Task OnRenewContext(AppDbContext ctx); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user