Add RenewContext
This commit is contained in:
@ -1,19 +1,43 @@
|
||||
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 readonly AppDbContext Context;
|
||||
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();
|
||||
}
|
||||
|
||||
private void OnLoaded(object sender, RoutedEventArgs evt) {
|
||||
RenewContext();
|
||||
}
|
||||
|
||||
protected override void OnClosed(EventArgs evt) {
|
||||
base.OnClosed(evt);
|
||||
Context.Dispose();
|
||||
}
|
||||
|
||||
abstract protected Task RenewContext();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user