From 9c6211e8e056715c043f356de5c218bf878ae765 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Wed, 8 Jun 2022 08:39:17 -0600 Subject: [PATCH] Improve UI speed when adding many books to queue at once. --- .../ProcessQueue/ProcessQueueControl.cs | 60 +++++++++++++++++-- .../ProcessQueue/TrackedQueue[T].cs | 9 +++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs b/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs index d6b9d322..7da03da5 100644 --- a/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs +++ b/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs @@ -77,27 +77,64 @@ namespace LibationWinForms.ProcessQueue CompletedCount = 0; } + private bool isBookInQueue(DataLayer.LibraryBook libraryBook) + => Queue.Any(b => b?.LibraryBook?.Book?.AudibleProductId == libraryBook.Book.AudibleProductId); + public void AddDownloadPdf(IEnumerable entries) { + List procs = new(); foreach (var entry in entries) - AddDownloadPdf(entry); + { + if (isBookInQueue(entry)) + continue; + + ProcessBook pbook = new(entry, Logger); + pbook.PropertyChanged += Pbook_DataAvailable; + pbook.AddDownloadPdf(); + procs.Add(pbook); + } + + AddToQueue(procs); } public void AddDownloadDecrypt(IEnumerable entries) { + List procs = new(); foreach (var entry in entries) - AddDownloadDecrypt(entry); + { + if (isBookInQueue(entry)) + continue; + + ProcessBook pbook = new(entry, Logger); + pbook.PropertyChanged += Pbook_DataAvailable; + pbook.AddDownloadDecryptBook(); + pbook.AddDownloadPdf(); + procs.Add(pbook); + } + + AddToQueue(procs); } public void AddConvertMp3(IEnumerable entries) { + List procs = new(); foreach (var entry in entries) - AddConvertMp3(entry); + { + if (isBookInQueue(entry)) + continue; + + ProcessBook pbook = new(entry, Logger); + pbook.PropertyChanged += Pbook_DataAvailable; + pbook.AddConvertToMp3(); + procs.Add(pbook); + } + + AddToQueue(procs); } public void AddDownloadPdf(DataLayer.LibraryBook libraryBook) { - if (Queue.Any(b => b?.LibraryBook?.Book?.AudibleProductId == libraryBook.Book.AudibleProductId)) + if (isBookInQueue(libraryBook)) return; ProcessBook pbook = new(libraryBook, Logger); @@ -108,7 +145,7 @@ namespace LibationWinForms.ProcessQueue public void AddDownloadDecrypt(DataLayer.LibraryBook libraryBook) { - if (Queue.Any(b => b?.LibraryBook?.Book?.AudibleProductId == libraryBook.Book.AudibleProductId)) + if (isBookInQueue(libraryBook)) return; ProcessBook pbook = new(libraryBook, Logger); @@ -120,7 +157,7 @@ namespace LibationWinForms.ProcessQueue public void AddConvertMp3(DataLayer.LibraryBook libraryBook) { - if (Queue.Any(b => b?.LibraryBook?.Book?.AudibleProductId == libraryBook.Book.AudibleProductId)) + if (isBookInQueue(libraryBook)) return; ProcessBook pbook = new(libraryBook, Logger); @@ -129,6 +166,17 @@ namespace LibationWinForms.ProcessQueue AddToQueue(pbook); } + private void AddToQueue(IEnumerable pbook) + { + syncContext.Post(_ => + { + Queue.Enqueue(pbook); + if (!Running) + QueueRunner = QueueLoop(); + }, + null); + } + private void AddToQueue(ProcessBook pbook) { syncContext.Post(_ => diff --git a/Source/LibationWinForms/ProcessQueue/TrackedQueue[T].cs b/Source/LibationWinForms/ProcessQueue/TrackedQueue[T].cs index 20bbe7e8..43c8ce98 100644 --- a/Source/LibationWinForms/ProcessQueue/TrackedQueue[T].cs +++ b/Source/LibationWinForms/ProcessQueue/TrackedQueue[T].cs @@ -226,5 +226,14 @@ namespace LibationWinForms.ProcessQueue QueuededCountChanged?.Invoke(this, _queued.Count); } } + + public void Enqueue(IEnumerable item) + { + lock (lockObject) + { + _queued.AddRange(item); + QueuededCountChanged?.Invoke(this, _queued.Count); + } + } } }