From 1ab628dee84901b9d2204d54dc6f35f06049a60f Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Sat, 14 May 2022 23:45:13 -0600 Subject: [PATCH] Better invocation. Post instead of Send --- Source/LibationWinForms/Form1.Liberate.cs | 4 +- .../ProcessQueue/ProcessQueueControl.cs | 54 +++++-------------- 2 files changed, 16 insertions(+), 42 deletions(-) diff --git a/Source/LibationWinForms/Form1.Liberate.cs b/Source/LibationWinForms/Form1.Liberate.cs index 30215723..da85747f 100644 --- a/Source/LibationWinForms/Form1.Liberate.cs +++ b/Source/LibationWinForms/Form1.Liberate.cs @@ -29,7 +29,9 @@ namespace LibationWinForms MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) - await Task.Run(() => processBookQueue1.AddConvertMp3(ApplicationServices.DbContexts.GetLibrary_Flat_NoTracking())); + await Task.Run(() => processBookQueue1.AddConvertMp3(ApplicationServices.DbContexts.GetLibrary_Flat_NoTracking() + .Where(lb=>lb.Book.UserDefinedItem.BookStatus is DataLayer.LiberatedStatus.Liberated))); + //Only Queue Liberated books for conversion. This isn't a perfect filter, but it's better than nothing. } } } diff --git a/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs b/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs index 0de13f3a..0a222160 100644 --- a/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs +++ b/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; -using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; @@ -13,7 +12,6 @@ namespace LibationWinForms.ProcessQueue { private TrackedQueue Queue = new(); private readonly LogMe Logger; - private SynchronizationContext SyncContext { get; } = SynchronizationContext.Current; private int QueuedCount { set @@ -40,7 +38,6 @@ namespace LibationWinForms.ProcessQueue } } - public Task QueueRunner { get; private set; } public bool Running => !QueueRunner?.IsCompleted ?? false; public ToolStripButton popoutBtn = new(); @@ -73,35 +70,20 @@ namespace LibationWinForms.ProcessQueue public void AddDownloadPdf(IEnumerable entries) { - Action> makeAll = (lb) => - { - foreach (var entry in entries) - AddDownloadPdf(entry); - }; - //IEnumerable are run on non-ui thread, so send collection to UI first - PassToUIThread(entries, makeAll); + foreach (var entry in entries) + AddDownloadPdf(entry); } public void AddDownloadDecrypt(IEnumerable entries) { - Action> makeAll = (lb) => - { - foreach (var entry in entries) - AddDownloadDecrypt(entry); - }; - //IEnumerable are run on non-ui thread, so send collection to UI first - PassToUIThread(entries, makeAll); + foreach (var entry in entries) + AddDownloadDecrypt(entry); } public void AddConvertMp3(IEnumerable entries) { - Action> makeAll = (lb) => - { - foreach (var entry in entries) - AddConvertMp3(entry); - }; - //IEnumerable are run on non-ui thread, so send collection to UI first - PassToUIThread(entries, makeAll); + foreach (var entry in entries) + AddConvertMp3(entry); } public void AddDownloadPdf(DataLayer.LibraryBook libraryBook) @@ -138,23 +120,14 @@ namespace LibationWinForms.ProcessQueue AddToQueue(pbook); } - private void PassToUIThread(IEnumerable libraryBooks, Action> onComplete) - { - void OnSendOrPostCallback(object asyncArgs) - { - onComplete((IEnumerable)asyncArgs); - } - SyncContext.Send(OnSendOrPostCallback, libraryBooks); - } - private void AddToQueue(ProcessBook pbook) { - Queue.Enqueue(pbook); - - if (!Running) + BeginInvoke(() => { - QueueRunner = QueueLoop(); - } + Queue.Enqueue(pbook); + if (!Running) + QueueRunner = QueueLoop(); + }); } DateTime StartintTime; @@ -176,7 +149,6 @@ namespace LibationWinForms.ProcessQueue else if (result == ProcessBookResult.FailedAbort) return; } - Queue_CompletedCountChanged(this, 0); counterTimer.Stop(); virtualFlowControl2.VirtualControlCount = Queue.Count; @@ -210,8 +182,8 @@ namespace LibationWinForms.ProcessQueue } private void UpdateProgressBar() { - toolStripProgressBar1.Maximum = Queue.Count; - toolStripProgressBar1.Value = Queue.Completed.Count; + toolStripProgressBar1.Maximum = Queue.Count; + toolStripProgressBar1.Value = Queue.Completed.Count; } private void cancelAllBtn_Click(object sender, EventArgs e)