From ffaccdef95d1b29e514409b9161010cb58ffc31d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fele?= Date: Sun, 15 Oct 2023 15:44:38 +0200 Subject: [PATCH] Check for updates every 10 minutes --- .../HostedServices/UpdaterHostedService.cs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/ChatPrisma/HostedServices/UpdaterHostedService.cs b/src/ChatPrisma/HostedServices/UpdaterHostedService.cs index 679a228..bb9905f 100644 --- a/src/ChatPrisma/HostedServices/UpdaterHostedService.cs +++ b/src/ChatPrisma/HostedServices/UpdaterHostedService.cs @@ -4,26 +4,26 @@ namespace ChatPrisma.HostedServices; -public class UpdaterHostedService(IUpdateManager updateManager, Application app) : IHostedService +public class UpdaterHostedService(IUpdateManager updateManager, Application app) : BackgroundService { - public async Task StartAsync(CancellationToken cancellationToken) + protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - var result = await updateManager.CheckForUpdatesAsync(cancellationToken); - if (result is { CanUpdate: true, LastVersion: not null }) + while (stoppingToken.IsCancellationRequested is false) { - await updateManager.PrepareUpdateAsync(result.LastVersion, cancellationToken: cancellationToken); - - var updateResult = MessageBox.Show("Update available!, Wanna update now?", "Title", MessageBoxButton.YesNo); - if (updateResult == MessageBoxResult.Yes) + var result = await updateManager.CheckForUpdatesAsync(stoppingToken); + if (result is { CanUpdate: true, LastVersion: not null }) { - updateManager.LaunchUpdater(result.LastVersion); - app.Shutdown(); + var updateResult = MessageBox.Show($"Update available {result.LastVersion}!, Wanna update now?", "Title", MessageBoxButton.YesNo); + if (updateResult == MessageBoxResult.Yes) + { + await updateManager.PrepareUpdateAsync(result.LastVersion, cancellationToken: stoppingToken); + updateManager.LaunchUpdater(result.LastVersion); + + app.Shutdown(); + } } - } - } - public Task StopAsync(CancellationToken cancellationToken) - { - return Task.CompletedTask; + await Task.Delay(TimeSpan.FromMinutes(10), stoppingToken); + } } }