44 lines
1.3 KiB
C#
44 lines
1.3 KiB
C#
using Elwig.Helpers;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Threading;
|
|
|
|
namespace Elwig.Windows {
|
|
public abstract class ContextWindow : Window {
|
|
|
|
protected AppDbContext Context { get; private set; }
|
|
protected bool LockContext { get; set; } = false;
|
|
|
|
private readonly DispatcherTimer ContextRenewTimer;
|
|
private static readonly int ContextRenewSec = 10;
|
|
|
|
public ContextWindow() : base() {
|
|
ContextRenewTimer = new DispatcherTimer();
|
|
ContextRenewTimer.Tick += new EventHandler(OnRenewContext);
|
|
ContextRenewTimer.Interval = new TimeSpan(0, 0, ContextRenewSec);
|
|
ContextRenewTimer.Start();
|
|
Context = new();
|
|
Loaded += OnLoaded;
|
|
}
|
|
|
|
private void OnRenewContext(object? sender, EventArgs evt) {
|
|
if (LockContext || !Context.HasBackendChanged) return;
|
|
Context.Dispose();
|
|
Context = new();
|
|
RenewContext().GetAwaiter().GetResult();
|
|
}
|
|
|
|
private void OnLoaded(object sender, RoutedEventArgs evt) {
|
|
RenewContext().GetAwaiter().GetResult();
|
|
}
|
|
|
|
protected override void OnClosed(EventArgs evt) {
|
|
base.OnClosed(evt);
|
|
Context.Dispose();
|
|
}
|
|
|
|
abstract protected Task RenewContext();
|
|
}
|
|
}
|