Fully remove processing queue code

This commit is contained in:
Matt Nadareski
2024-12-19 00:37:56 -05:00
parent cebbeb264f
commit 216c6de970
3 changed files with 12 additions and 117 deletions

View File

@@ -88,6 +88,7 @@
- Simplify ProcessLogLine method
- Use explicit InvokeAsync when possible
- (EXPERIMENTAL) Skip log line queue
- Fully remove processing queue code
### 3.2.4 (2024-11-24)

View File

@@ -1,83 +0,0 @@
using System;
#if NET40_OR_GREATER || NETCOREAPP
using System.Collections.Concurrent;
#endif
using System.Threading;
using System.Threading.Tasks;
namespace MPF.Frontend
{
public sealed 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>
/// Cancellation method for the processing task
/// </summary>
private readonly CancellationTokenSource _tokenSource;
public ProcessingQueue(Action<T> customProcessing)
{
_internalQueue = [];
_customProcessing = customProcessing;
_tokenSource = new CancellationTokenSource();
#if NET20 || NET35
Task.Run(() => ProcessQueue());
#elif NET40
Task.Factory.StartNew(() => ProcessQueue());
#else
Task.Run(() => ProcessQueue(), _tokenSource.Token);
#endif
}
/// <summary>
/// Dispose the current instance
/// </summary>
public void Dispose() => _tokenSource.Cancel();
/// <summary>
/// Enqueue a new item for processing
/// </summary>
/// <param name="item"></param>
public void Enqueue(T? item)
{
// Only accept new data when not cancelled
if (item != null && !_tokenSource.IsCancellationRequested)
_internalQueue.Enqueue(item);
}
/// <summary>
/// Process
/// </summary>
private void ProcessQueue()
{
while (true)
{
// Nothing in the queue means we get to idle
if (_internalQueue.Count == 0)
{
if (_tokenSource.IsCancellationRequested)
break;
Thread.Sleep(25);
continue;
}
// Get the next item from the queue
if (!_internalQueue.TryDequeue(out var nextItem))
continue;
// Invoke the lambda, if possible
_customProcessing?.Invoke(nextItem);
}
}
}
}

View File

@@ -16,11 +16,6 @@ namespace MPF.UI.UserControls
/// </summary>
private readonly FlowDocument _document;
/// <summary>
/// Queue of items that need to be logged
/// </summary>
//private readonly ProcessingQueue<LogLine> _logQueue;
/// <summary>
/// Paragraph backing the log
/// </summary>
@@ -49,9 +44,6 @@ namespace MPF.UI.UserControls
_paragraph = new Paragraph();
_document.Blocks.Add(_paragraph);
// Setup the processing queue
//_logQueue = new ProcessingQueue<LogLine>(ProcessLogLine);
// Add handlers
OutputViewer!.SizeChanged += OutputViewerSizeChanged;
Output!.TextChanged += OnTextChanged;
@@ -75,11 +67,18 @@ namespace MPF.UI.UserControls
if (text == null)
return;
// Enqueue the text
//_logQueue.Enqueue(new LogLine(text, logLevel));
// Create a new log line
var logLine = new LogLine(text, logLevel);
// EXPERIMENTAL - Directly process the log line
ProcessLogLine(new LogLine(text, logLevel));
#if NET40
Dispatcher.Invoke(() =>
#else
Dispatcher.InvokeAsync(() =>
#endif
{
var run = logLine.GenerateRun();
_paragraph.Inlines.Add(run);
});
}
/// <summary>
@@ -121,28 +120,6 @@ namespace MPF.UI.UserControls
}
}
/// <summary>
/// Process the log lines in the queue
/// </summary>
/// <param name="logLine">LogLine item to process</param>
internal void ProcessLogLine(LogLine logLine)
{
// Null text gets ignored
string nextText = logLine.Text;
if (nextText == null)
return;
#if NET40
Dispatcher.Invoke(() =>
#else
Dispatcher.InvokeAsync(() =>
#endif
{
var run = logLine.GenerateRun();
_paragraph.Inlines.Add(run);
});
}
#endregion
#region Helpers