2021-03-29 10:23:34 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Concurrent;
|
2021-03-31 21:26:55 -07:00
|
|
|
|
using System.Threading;
|
2021-03-29 10:23:34 -07:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
2021-09-29 11:48:37 -07:00
|
|
|
|
namespace MPF.Core.Data
|
2021-03-29 10:23:34 -07:00
|
|
|
|
{
|
2021-03-29 10:50:43 -07:00
|
|
|
|
public class ProcessingQueue<T> : IDisposable
|
2021-03-29 10:23:34 -07:00
|
|
|
|
{
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
|
2021-03-31 21:26:55 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Cancellation method for the processing task
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private readonly CancellationTokenSource TokenSource;
|
|
|
|
|
|
|
2021-03-29 10:23:34 -07:00
|
|
|
|
public ProcessingQueue(Action<T> customProcessing)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.InternalQueue = new ConcurrentQueue<T>();
|
|
|
|
|
|
this.CustomProcessing = customProcessing;
|
2021-03-31 21:26:55 -07:00
|
|
|
|
this.TokenSource = new CancellationTokenSource();
|
2021-04-01 15:10:43 -07:00
|
|
|
|
Task.Run(() => ProcessQueue(), this.TokenSource.Token);
|
2021-03-29 10:50:43 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Dispose the current instance
|
|
|
|
|
|
/// </summary>
|
2021-11-26 14:06:57 -08:00
|
|
|
|
public void Dispose() => this.TokenSource.Cancel();
|
2021-03-29 10:50:43 -07:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Enqueue a new item for processing
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="item"></param>
|
|
|
|
|
|
public void Enqueue(T item)
|
|
|
|
|
|
{
|
2021-04-01 19:08:39 -07:00
|
|
|
|
// Only accept new data when not cancelled
|
2021-04-01 15:10:43 -07:00
|
|
|
|
if (!this.TokenSource.IsCancellationRequested)
|
|
|
|
|
|
this.InternalQueue.Enqueue(item);
|
2021-03-29 10:23:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Process
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void ProcessQueue()
|
|
|
|
|
|
{
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Nothing in the queue means we get to idle
|
2021-03-31 21:26:55 -07:00
|
|
|
|
if (this.InternalQueue.Count == 0)
|
2021-04-01 19:08:39 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (this.TokenSource.IsCancellationRequested)
|
|
|
|
|
|
break;
|
2021-06-14 22:37:47 -07:00
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(10);
|
2021-03-29 10:23:34 -07:00
|
|
|
|
continue;
|
2021-04-01 19:08:39 -07:00
|
|
|
|
}
|
2021-03-29 10:23:34 -07:00
|
|
|
|
|
|
|
|
|
|
// Get the next item from the queue
|
2021-03-31 21:26:55 -07:00
|
|
|
|
if (!this.InternalQueue.TryDequeue(out T nextItem))
|
2021-03-29 10:23:34 -07:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Invoke the lambda, if possible
|
|
|
|
|
|
this.CustomProcessing?.Invoke(nextItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|