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, MessageBoxButtons.YesNo,
MessageBoxIcon.Warning); MessageBoxIcon.Warning);
if (result == DialogResult.Yes) 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.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@ -13,7 +12,6 @@ namespace LibationWinForms.ProcessQueue
{ {
private TrackedQueue<ProcessBook> Queue = new(); private TrackedQueue<ProcessBook> Queue = new();
private readonly LogMe Logger; private readonly LogMe Logger;
private SynchronizationContext SyncContext { get; } = SynchronizationContext.Current;
private int QueuedCount private int QueuedCount
{ {
set set
@ -40,7 +38,6 @@ namespace LibationWinForms.ProcessQueue
} }
} }
public Task QueueRunner { get; private set; } public Task QueueRunner { get; private set; }
public bool Running => !QueueRunner?.IsCompleted ?? false; public bool Running => !QueueRunner?.IsCompleted ?? false;
public ToolStripButton popoutBtn = new(); public ToolStripButton popoutBtn = new();
@ -73,35 +70,20 @@ namespace LibationWinForms.ProcessQueue
public void AddDownloadPdf(IEnumerable<DataLayer.LibraryBook> entries) public void AddDownloadPdf(IEnumerable<DataLayer.LibraryBook> entries)
{ {
Action<IEnumerable<DataLayer.LibraryBook>> makeAll = (lb) => foreach (var entry in entries)
{ AddDownloadPdf(entry);
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) public void AddDownloadDecrypt(IEnumerable<DataLayer.LibraryBook> entries)
{ {
Action<IEnumerable<DataLayer.LibraryBook>> makeAll = (lb) => foreach (var entry in entries)
{ AddDownloadDecrypt(entry);
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) public void AddConvertMp3(IEnumerable<DataLayer.LibraryBook> entries)
{ {
Action<IEnumerable<DataLayer.LibraryBook>> makeAll = (lb) => foreach (var entry in entries)
{ AddConvertMp3(entry);
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) public void AddDownloadPdf(DataLayer.LibraryBook libraryBook)
@ -138,23 +120,14 @@ namespace LibationWinForms.ProcessQueue
AddToQueue(pbook); 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) private void AddToQueue(ProcessBook pbook)
{ {
Queue.Enqueue(pbook); BeginInvoke(() =>
if (!Running)
{ {
QueueRunner = QueueLoop(); Queue.Enqueue(pbook);
} if (!Running)
QueueRunner = QueueLoop();
});
} }
DateTime StartintTime; DateTime StartintTime;
@ -176,7 +149,6 @@ namespace LibationWinForms.ProcessQueue
else if (result == ProcessBookResult.FailedAbort) else if (result == ProcessBookResult.FailedAbort)
return; return;
} }
Queue_CompletedCountChanged(this, 0); Queue_CompletedCountChanged(this, 0);
counterTimer.Stop(); counterTimer.Stop();
virtualFlowControl2.VirtualControlCount = Queue.Count; virtualFlowControl2.VirtualControlCount = Queue.Count;
@ -210,8 +182,8 @@ namespace LibationWinForms.ProcessQueue
} }
private void UpdateProgressBar() private void UpdateProgressBar()
{ {
toolStripProgressBar1.Maximum = Queue.Count; toolStripProgressBar1.Maximum = Queue.Count;
toolStripProgressBar1.Value = Queue.Completed.Count; toolStripProgressBar1.Value = Queue.Completed.Count;
} }
private void cancelAllBtn_Click(object sender, EventArgs e) private void cancelAllBtn_Click(object sender, EventArgs e)