diff --git a/Terminal.Gui/ConsoleDrivers/NetDriver.cs b/Terminal.Gui/ConsoleDrivers/NetDriver.cs index 0a3b1e6105..dacb6b0f62 100644 --- a/Terminal.Gui/ConsoleDrivers/NetDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/NetDriver.cs @@ -1690,7 +1690,7 @@ internal class NetMainLoop : IMainLoopDriver private readonly CancellationTokenSource _inputHandlerTokenSource = new (); private readonly Queue _resultQueue = new (); private readonly ManualResetEventSlim _waitForProbe = new (false); - private CancellationTokenSource _eventReadyTokenSource = new (); + private readonly CancellationTokenSource _eventReadyTokenSource = new (); private MainLoop _mainLoop; /// Initializes the class with the console driver. @@ -1742,14 +1742,13 @@ bool IMainLoopDriver.EventsPending () _eventReady.Reset (); } + _eventReadyTokenSource.Token.ThrowIfCancellationRequested (); + if (!_eventReadyTokenSource.IsCancellationRequested) { return _resultQueue.Count > 0 || _mainLoop.CheckTimersAndIdleHandlers (out _); } - _eventReadyTokenSource.Dispose (); - _eventReadyTokenSource = new CancellationTokenSource (); - return true; } @@ -1806,26 +1805,21 @@ private void NetInputHandler () return; } + _inputHandlerTokenSource.Token.ThrowIfCancellationRequested (); + if (_resultQueue.Count == 0) { _resultQueue.Enqueue (_netEvents.DequeueInput ()); } - try + while (_resultQueue.Count > 0 && _resultQueue.Peek () is null) { - while (_resultQueue.Peek () is null) - { - _resultQueue.Dequeue (); - } - - if (_resultQueue.Count > 0) - { - _eventReady.Set (); - } + _resultQueue.Dequeue (); } - catch (InvalidOperationException) + + if (_resultQueue.Count > 0) { - // Ignore + _eventReady.Set (); } } }