Improve UI speed when adding many books to queue at once.

This commit is contained in:
Michael Bucari-Tovo 2022-06-08 08:39:17 -06:00
parent 0729e4ab09
commit 9c6211e8e0
2 changed files with 63 additions and 6 deletions

View File

@ -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<DataLayer.LibraryBook> entries)
{
List<ProcessBook> 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<DataLayer.LibraryBook> entries)
{
List<ProcessBook> 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<DataLayer.LibraryBook> entries)
{
List<ProcessBook> 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<ProcessBook> pbook)
{
syncContext.Post(_ =>
{
Queue.Enqueue(pbook);
if (!Running)
QueueRunner = QueueLoop();
},
null);
}
private void AddToQueue(ProcessBook pbook)
{
syncContext.Post(_ =>

View File

@ -226,5 +226,14 @@ namespace LibationWinForms.ProcessQueue
QueuededCountChanged?.Invoke(this, _queued.Count);
}
}
public void Enqueue(IEnumerable<T> item)
{
lock (lockObject)
{
_queued.AddRange(item);
QueuededCountChanged?.Invoke(this, _queued.Count);
}
}
}
}