using PamhagenSysCtrl.Helpers; using System.IO; using System.Windows; namespace PamhagenSysCtrl.Dialogs { public partial class UpdateDialog : Window { private readonly string Url; private readonly CancellationTokenSource Cancellation = new(); public UpdateDialog(string version, string url, long size) { Url = url; InitializeComponent(); VersionText.Text = version; SizeText.Text = Math.Ceiling(size / 1024d / 1024d).ToString("N0"); } private void OnClosed(object? sender, EventArgs evt) { Cancellation.Cancel(); Cancellation.Dispose(); } private async void InstallButton_Click(object sender, RoutedEventArgs evt) { Description.Visibility = Visibility.Collapsed; DownloadPanel.Visibility = Visibility.Visible; InstallButton.IsEnabled = false; try { var progress = new Progress(value => DownloadProgress.Value = value * 100); var filename = Path.Combine(App.TempPath, $"PamhagenSysCtrl-{VersionText.Text}.msi"); await UpdateService.DownloadInstaller(Url, filename, progress, Cancellation.Token); StatusText.Text = "Installer wird gestartet..."; UpdateService.StartInstaller(filename); DialogResult = true; } catch (OperationCanceledException) { // Closing the dialog cancels an active download. } catch (Exception exc) { MessageBox.Show($"Das Update konnte nicht installiert werden:\n\n{exc.Message}", "Update installieren", MessageBoxButton.OK, MessageBoxImage.Error); Description.Visibility = Visibility.Visible; DownloadPanel.Visibility = Visibility.Collapsed; InstallButton.IsEnabled = true; } } } }