Better invocation. Post instead of Send

This commit is contained in:
Michael Bucari-Tovo 2022-05-14 23:45:13 -06:00
parent b24df24b10
commit 1ab628dee8
2 changed files with 16 additions and 42 deletions

View File

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

View File

@ -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<ProcessBook> 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();
@ -72,36 +69,21 @@ namespace LibationWinForms.ProcessQueue
}
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)
{
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)
{
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)
@ -138,23 +120,14 @@ 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);
if (!Running)
BeginInvoke(() =>
{
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;