From 216c6de9704744bece8198271e405f290ce816ce Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 19 Dec 2024 00:37:56 -0500 Subject: [PATCH] Fully remove processing queue code --- CHANGELIST.md | 1 + MPF.Frontend/ProcessingQueue.cs | 83 --------------------------- MPF.UI/UserControls/LogOutput.xaml.cs | 45 ++++----------- 3 files changed, 12 insertions(+), 117 deletions(-) delete mode 100644 MPF.Frontend/ProcessingQueue.cs diff --git a/CHANGELIST.md b/CHANGELIST.md index 4ea1e1b9..8f8d9b26 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -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) diff --git a/MPF.Frontend/ProcessingQueue.cs b/MPF.Frontend/ProcessingQueue.cs deleted file mode 100644 index 24019ce1..00000000 --- a/MPF.Frontend/ProcessingQueue.cs +++ /dev/null @@ -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 : IDisposable - { - /// - /// Internal queue to hold data to process - /// - private readonly ConcurrentQueue _internalQueue; - - /// - /// Custom processing step for dequeued data - /// - private readonly Action _customProcessing; - - /// - /// Cancellation method for the processing task - /// - private readonly CancellationTokenSource _tokenSource; - - public ProcessingQueue(Action 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 - } - - /// - /// Dispose the current instance - /// - public void Dispose() => _tokenSource.Cancel(); - - /// - /// Enqueue a new item for processing - /// - /// - public void Enqueue(T? item) - { - // Only accept new data when not cancelled - if (item != null && !_tokenSource.IsCancellationRequested) - _internalQueue.Enqueue(item); - } - - /// - /// Process - /// - 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); - } - } - } -} diff --git a/MPF.UI/UserControls/LogOutput.xaml.cs b/MPF.UI/UserControls/LogOutput.xaml.cs index c8a55203..009086b2 100644 --- a/MPF.UI/UserControls/LogOutput.xaml.cs +++ b/MPF.UI/UserControls/LogOutput.xaml.cs @@ -16,11 +16,6 @@ namespace MPF.UI.UserControls /// private readonly FlowDocument _document; - /// - /// Queue of items that need to be logged - /// - //private readonly ProcessingQueue _logQueue; - /// /// Paragraph backing the log /// @@ -49,9 +44,6 @@ namespace MPF.UI.UserControls _paragraph = new Paragraph(); _document.Blocks.Add(_paragraph); - // Setup the processing queue - //_logQueue = new ProcessingQueue(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); + }); } /// @@ -121,28 +120,6 @@ namespace MPF.UI.UserControls } } - /// - /// Process the log lines in the queue - /// - /// LogLine item to process - 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