Consolidate thumbnails behavior into new Thumbnails class

This commit is contained in:
John Lewin 2018-06-21 10:50:40 -07:00
parent b98bb39136
commit 6dea15a6d7
2 changed files with 3 additions and 71 deletions

View file

@ -163,10 +163,6 @@ namespace MatterHackers.MatterControl
public static Func<string, Task<Dictionary<string, string>>> GetProfileHistory;
private readonly static object thumbsLock = new object();
private Queue<Func<Task>> queuedThumbCallbacks = new Queue<Func<Task>>();
public async Task SetActivePrinter(PrinterConfig printer, bool allowChangedEvent = true)
{
var initialPrinter = this.ActivePrinter;
@ -279,69 +275,6 @@ namespace MatterHackers.MatterControl
}*/
}
private AutoResetEvent thumbGenResetEvent = new AutoResetEvent(false);
Task thumbnailGenerator = null;
internal void QueueForGeneration(Func<Task> func)
{
lock (thumbsLock)
{
if (thumbnailGenerator == null)
{
// Spin up a new thread once needed
thumbnailGenerator = Task.Run((Action)ThumbGeneration);
}
queuedThumbCallbacks.Enqueue(func);
thumbGenResetEvent.Set();
}
}
private async void ThumbGeneration()
{
Thread.CurrentThread.Name = $"ThumbnailGeneration";
while (!this.ApplicationExiting)
{
Thread.Sleep(100);
try
{
if (queuedThumbCallbacks.Count > 0)
{
Func<Task> callback;
lock (thumbsLock)
{
callback = queuedThumbCallbacks.Dequeue();
}
await callback();
}
else
{
// Process until queuedThumbCallbacks is empty then wait for new tasks via QueueForGeneration
thumbGenResetEvent.WaitOne();
}
}
catch (AppDomainUnloadedException)
{
return;
}
catch (ThreadAbortException)
{
return;
}
catch (Exception ex)
{
Console.WriteLine("Error generating thumbnail: " + ex.Message);
}
}
// Null task reference on exit
thumbnailGenerator = null;
}
public static Func<PrinterInfo, string, Task<PrinterSettings>> GetPrinterProfileAsync;
public static Func<string, IProgress<ProgressStatus>, Task> SyncPrinterProfiles;
public static Func<Task<OemProfileDictionary>> GetPublicProfileList;
@ -1103,7 +1036,7 @@ namespace MatterHackers.MatterControl
// Ensure all threads shutdown gracefully on close
// Release any waiting generator threads
thumbGenResetEvent?.Set();
this.Thumbnails.Shutdown();
// Kill all long running tasks (this will release the silcing thread if running)
foreach (var task in Tasks.RunningTasks)
@ -1302,8 +1235,7 @@ namespace MatterHackers.MatterControl
public async void OnApplicationClosed()
{
// Release the waiting ThumbnailGeneration task so it can shutdown gracefully
thumbGenResetEvent?.Set();
this.Thumbnails.Shutdown();
// Save changes before close
if (this.ActivePrinter != null