Move some classes around and cleanup

This commit is contained in:
Matt Nadareski
2021-03-29 11:55:29 -07:00
parent 11ce78fca2
commit d963dbbd5f
11 changed files with 54 additions and 49 deletions

View File

@@ -0,0 +1,68 @@
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
namespace MPF.Data
{
public class ProcessingQueue<T> : IDisposable
{
/// <summary>
/// Internal queue to hold data to process
/// </summary>
private readonly ConcurrentQueue<T> InternalQueue;
/// <summary>
/// Custom processing step for dequeued data
/// </summary>
private readonly Action<T> CustomProcessing;
/// <summary>
/// Internal processing task for dequeueing
/// </summary>
private readonly Task ProcessingTask;
public ProcessingQueue(Action<T> customProcessing)
{
this.InternalQueue = new ConcurrentQueue<T>();
this.CustomProcessing = customProcessing;
this.ProcessingTask = Task.Run(() => ProcessQueue());
}
/// <summary>
/// Dispose the current instance
/// </summary>
public void Dispose()
{
this.ProcessingTask.Dispose();
}
/// <summary>
/// Enqueue a new item for processing
/// </summary>
/// <param name="item"></param>
public void Enqueue(T item)
{
this.InternalQueue.Enqueue(item);
}
/// <summary>
/// Process
/// </summary>
private void ProcessQueue()
{
while (true)
{
// Nothing in the queue means we get to idle
if (InternalQueue.Count == 0)
continue;
// Get the next item from the queue
if (!InternalQueue.TryDequeue(out T nextItem))
continue;
// Invoke the lambda, if possible
this.CustomProcessing?.Invoke(nextItem);
}
}
}
}