| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using System.Collections.Concurrent;
- namespace Terminal.Gui.Drivers;
- /// <summary>
- /// Base class for reading console input in perpetual loop.
- /// The <see cref="Peek"/> and <see cref="Read"/> methods are executed
- /// on the input thread created by <see cref="MainLoopCoordinator{TInputRecord}.StartInputTaskAsync"/>.
- /// </summary>
- /// <typeparam name="TInputRecord"></typeparam>
- public abstract class InputImpl<TInputRecord> : IInput<TInputRecord>
- {
- private ConcurrentQueue<TInputRecord>? _inputQueue;
- /// <summary>
- /// Determines how to get the current system type, adjust
- /// in unit tests to simulate specific timings.
- /// </summary>
- public Func<DateTime> Now { get; set; } = () => DateTime.Now;
- /// <inheritdoc/>
- public CancellationTokenSource? ExternalCancellationTokenSource { get; set; }
- /// <inheritdoc/>
- public void Initialize (ConcurrentQueue<TInputRecord> inputQueue) { _inputQueue = inputQueue; }
- /// <inheritdoc/>
- public void Run (CancellationToken runCancellationToken)
- {
- // Create a linked token source if we have an external one
- CancellationTokenSource? linkedCts = null;
- CancellationToken effectiveToken = runCancellationToken;
- if (ExternalCancellationTokenSource != null)
- {
- linkedCts = CancellationTokenSource.CreateLinkedTokenSource (runCancellationToken, ExternalCancellationTokenSource.Token);
- effectiveToken = linkedCts.Token;
- }
- try
- {
- if (_inputQueue == null)
- {
- throw new ("Cannot run input before Initialization");
- }
- do
- {
- while (Peek ())
- {
- foreach (TInputRecord r in Read ())
- {
- _inputQueue.Enqueue (r);
- }
- }
- effectiveToken.ThrowIfCancellationRequested ();
- // Throttle the input loop to avoid CPU spinning when no input is available
- // This is especially important when multiple ApplicationImpl instances are created
- // in parallel tests without calling Shutdown() - prevents thread pool exhaustion
- Task.Delay (20, effectiveToken).Wait (effectiveToken);
- }
- while (!effectiveToken.IsCancellationRequested);
- }
- catch (OperationCanceledException)
- { }
- finally
- {
- Logging.Trace ("Stopping input processing");
- linkedCts?.Dispose ();
- }
- }
- /// <summary>
- /// When implemented in a derived class, returns true if there is data available
- /// to read from console.
- /// </summary>
- /// <returns></returns>
- public abstract bool Peek ();
- /// <summary>
- /// Returns the available data without blocking, called when <see cref="Peek"/>
- /// returns <see langword="true"/>.
- /// </summary>
- /// <returns></returns>
- public abstract IEnumerable<TInputRecord> Read ();
- /// <inheritdoc/>
- public virtual void Dispose () { }
- }
|