Add Utils.RunBackground
This commit is contained in:
		| @@ -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); | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -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, | ||||
|   | ||||
| @@ -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<Task> 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; } | ||||
|   | ||||
| @@ -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(); | ||||
|             }); | ||||
|         } | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user