From 483657911d5f3ad96716e19d323529a5527dc2a1 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Thu, 9 Mar 2023 23:02:54 +0100 Subject: [PATCH] Add Utils.RunBackground --- WGneu/App.xaml.cs | 6 +++--- WGneu/Documents/Pdf.cs | 2 +- WGneu/Utils.cs | 10 ++++++++++ WGneu/Windows/MainWindow.xaml.cs | 12 ++++-------- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/WGneu/App.xaml.cs b/WGneu/App.xaml.cs index 14f9039..72520b5 100644 --- a/WGneu/App.xaml.cs +++ b/WGneu/App.xaml.cs @@ -19,13 +19,13 @@ namespace WGneu { } protected override void OnStartup(StartupEventArgs e) { - Task.Run(() => Documents.Html.Init(PrintingReadyChanged)); - Task.Run(() => Documents.Pdf.Init(PrintingReadyChanged)); + Utils.RunBackground("HTML Initialization", () => Documents.Html.Init(PrintingReadyChanged)); + Utils.RunBackground("PDF Initialization", () => Documents.Pdf.Init(PrintingReadyChanged)); base.OnStartup(e); } protected override void OnExit(ExitEventArgs e) { - Task.Run(() => Documents.Pdf.Close()); + Utils.RunBackground("PDF Close", () => Documents.Pdf.Close()); base.OnExit(e); } diff --git a/WGneu/Documents/Pdf.cs b/WGneu/Documents/Pdf.cs index 652861f..30bb3fb 100644 --- a/WGneu/Documents/Pdf.cs +++ b/WGneu/Documents/Pdf.cs @@ -39,7 +39,7 @@ namespace WGneu.Documents { if (Browser == null) throw new InvalidOperationException("The puppeteer engine has not been initialized yet"); using var page = await Browser.NewPageAsync(); page.Console += OnConsole; - await page.GoToAsync("file://" + htmlPath); + await page.GoToAsync($"file://{htmlPath}"); await page.EvaluateFunctionAsync("async () => { await window.PagedPolyfill.preview(); }"); await page.PdfAsync(pdfPath, new() { PreferCSSPageSize = true, diff --git a/WGneu/Utils.cs b/WGneu/Utils.cs index 946740e..b214b23 100644 --- a/WGneu/Utils.cs +++ b/WGneu/Utils.cs @@ -46,6 +46,16 @@ namespace WGneu { return a.Select(ch => ch - '0').Aggregate((sum, n) => (sum * 10 + n) % b); } + public static void RunBackground(string title, Func a) { + Task.Run(async () => { + try { + await a(); + } catch (Exception e) { + MessageBox.Show(e.ToString(), title, MessageBoxButton.OK, MessageBoxImage.Error); + } + }); + } + public sealed class TemporaryFile : IDisposable { private int Usages = 0; public string FilePath { get; private set; } diff --git a/WGneu/Windows/MainWindow.xaml.cs b/WGneu/Windows/MainWindow.xaml.cs index 489f96a..465fbfd 100644 --- a/WGneu/Windows/MainWindow.xaml.cs +++ b/WGneu/Windows/MainWindow.xaml.cs @@ -43,14 +43,10 @@ namespace WGneu.Windows { } private void Button4_Click(object sender, EventArgs e) { - Task.Run(async () => { - try { - using var letter = new BusinessLetter("Test Dokument", _context.Members.First()); - await letter.Generate(); - letter.Show(); - } catch (Exception e) { - MessageBox.Show(e.ToString(), "PDF Generation", MessageBoxButton.OK, MessageBoxImage.Error); - } + Utils.RunBackground("PDF Generation", async () => { + using var letter = new BusinessLetter("Test Dokument", _context.Members.First()); + await letter.Generate(); + letter.Show(); }); } }