Make thread safe and integrate with Libation UI

This commit is contained in:
Michael Bucari-Tovo 2022-05-14 14:39:46 -06:00
parent 50c35ed519
commit 73a5d76503
5 changed files with 66 additions and 26 deletions

View file

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
@ -12,7 +13,7 @@ namespace LibationWinForms.ProcessQueue
{
private TrackedQueue<ProcessBook> Queue = new();
private readonly LogMe Logger;
private SynchronizationContext SyncContext { get; } = SynchronizationContext.Current;
private int QueuedCount
{
set
@ -70,16 +71,48 @@ namespace LibationWinForms.ProcessQueue
CompletedCount = 0;
}
public void AddDownloadPdf(IEnumerable<DataLayer.LibraryBook> entries)
{
Action<IEnumerable<DataLayer.LibraryBook>> makeAll = (lb) =>
{
foreach (var entry in entries)
AddDownloadPdf(entry);
};
//IEnumerable<DataLayer.LibraryBook> are run on non-ui thread, so send collection to UI first
PassToUIThread(entries, makeAll);
}
public void AddDownloadDecrypt(IEnumerable<DataLayer.LibraryBook> entries)
{
foreach (var entry in entries)
AddDownloadDecrypt(entry);
Action<IEnumerable<DataLayer.LibraryBook>> makeAll = (lb) =>
{
foreach (var entry in entries)
AddDownloadDecrypt(entry);
};
//IEnumerable<DataLayer.LibraryBook> are run on non-ui thread, so send collection to UI first
PassToUIThread(entries, makeAll);
}
public void AddConvertMp3(IEnumerable<DataLayer.LibraryBook> entries)
{
foreach (var entry in entries)
AddConvertMp3(entry);
Action<IEnumerable<DataLayer.LibraryBook>> makeAll = (lb) =>
{
foreach (var entry in entries)
AddConvertMp3(entry);
};
//IEnumerable<DataLayer.LibraryBook> are run on non-ui thread, so send collection to UI first
PassToUIThread(entries, makeAll);
}
public void AddDownloadPdf(DataLayer.LibraryBook libraryBook)
{
if (Queue.Any(b => b?.LibraryBook?.Book?.AudibleProductId == libraryBook.Book.AudibleProductId))
return;
ProcessBook pbook = new(libraryBook, Logger);
pbook.PropertyChanged += Pbook_DataAvailable;
pbook.AddDownloadPdf();
AddToQueue(pbook);
}
public void AddDownloadDecrypt(DataLayer.LibraryBook libraryBook)
@ -105,6 +138,15 @@ namespace LibationWinForms.ProcessQueue
AddToQueue(pbook);
}
private void PassToUIThread(IEnumerable<DataLayer.LibraryBook> libraryBooks, Action<IEnumerable<DataLayer.LibraryBook>> onComplete)
{
void OnSendOrPostCallback(object asyncArgs)
{
onComplete((IEnumerable<DataLayer.LibraryBook>)asyncArgs);
}
SyncContext.Send(OnSendOrPostCallback, libraryBooks);
}
private void AddToQueue(ProcessBook pbook)
{
Queue.Enqueue(pbook);
@ -164,8 +206,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)