I am working on a Windows MAUI application. Simply: I have a database, from which I load data upon app opening, and I want to save the new data when the app is closed.
What I am currently doing:
protected override Window CreateWindow(IActivationState? activationState)
{
Window window = base.CreateWindow(activationState);
window.Destroying += OnAppWindowDestroying;
return window;
}
private async void OnAppWindowDestroying(object? sender, EventArgs e)
{
await Task.Delay(2000); // Database write operation goes here
}
The issue:
The app process is terminated before the database operations are terminated and my data are not persisted. So it seems I am not using the correct approach.
I also tried using Windows specific event like so:
.ConfigureLifecycleEvents(events =>
{
#if WINDOWS
events.AddWindows(windows => windows
.OnClosed(async (window, args) => await Task.Delay(2000)// database write operation goes here );
#endif
});
I guess the question is:
Is there anyway to delay a Windows App closing ? If not, then what is the correct way to perform a database operation upon app closing, launching a new process ?
Thanks for some insight here