https://stackoverflow.com/questions/15666824/entity-framework-and-calling-context-dispose https://blog.jongallant.com/2012/10/do-i-have-to-call-dispose-on-dbcontext/
52 lines
1.5 KiB
C#
52 lines
1.5 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 {
|
|
|
|
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;
|
|
}
|
|
|
|
public async Task HintContextChange() {
|
|
_renewPending = true;
|
|
if (LockContext) return;
|
|
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();
|
|
}
|
|
|
|
protected async Task RenewContext() {
|
|
if (!_renewPending) return;
|
|
Context = new();
|
|
await OnRenewContext();
|
|
_renewPending = false;
|
|
}
|
|
|
|
abstract protected Task OnRenewContext();
|
|
}
|
|
}
|