Add Utils.RunBackground

This commit is contained in:
2023-03-09 23:02:54 +01:00
parent 54ba861b0e
commit 483657911d
4 changed files with 18 additions and 12 deletions

View File

@ -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);
}

View File

@ -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,

View File

@ -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; }

View File

@ -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();
});
}
}