diff --git a/Terminal.Gui/ConsoleDrivers/CursesDriver/UnixMainLoop.cs b/Terminal.Gui/ConsoleDrivers/CursesDriver/UnixMainLoop.cs index feccf6126..64dbb3b18 100644 --- a/Terminal.Gui/ConsoleDrivers/CursesDriver/UnixMainLoop.cs +++ b/Terminal.Gui/ConsoleDrivers/CursesDriver/UnixMainLoop.cs @@ -174,12 +174,12 @@ namespace Terminal.Gui { bool IMainLoopDriver.EventsPending (bool wait) { + UpdatePollMap (); + if (CheckTimers (wait, out var pollTimeout)) { return true; } - UpdatePollMap (); - var n = poll (pollmap, (uint)pollmap.Length, pollTimeout); if (n == KEY_RESIZE) { diff --git a/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeMainLoop.cs b/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeMainLoop.cs index 42c9ea1e0..d59072972 100644 --- a/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeMainLoop.cs +++ b/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeMainLoop.cs @@ -58,24 +58,39 @@ namespace Terminal.Gui { } bool IMainLoopDriver.EventsPending (bool wait) + { + keyResult = null; + waitForProbe.Set (); + + if (CheckTimers (wait, out var waitTimeout)) { + return true; + } + + keyReady.WaitOne (waitTimeout); + return keyResult.HasValue; + } + + bool CheckTimers (bool wait, out int waitTimeout) { long now = DateTime.UtcNow.Ticks; - int waitTimeout; if (mainLoop.timeouts.Count > 0) { waitTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond); if (waitTimeout < 0) return true; - } else + } else { waitTimeout = -1; + } if (!wait) waitTimeout = 0; - keyResult = null; - waitForProbe.Set (); - keyReady.WaitOne (waitTimeout); - return keyResult.HasValue; + int ic; + lock (mainLoop.idleHandlers) { + ic = mainLoop.idleHandlers.Count; + } + + return ic > 0; } void IMainLoopDriver.MainIteration () diff --git a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs index 02dd213de..c6dc0a684 100644 --- a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs @@ -1840,13 +1840,13 @@ namespace Terminal.Gui { bool IMainLoopDriver.EventsPending (bool wait) { + waitForProbe.Set (); + winChange.Set (); + if (CheckTimers (wait, out var waitTimeout)) { return true; } - waitForProbe.Set (); - winChange.Set (); - try { if (!tokenSource.IsCancellationRequested) { eventReady.Wait (waitTimeout, tokenSource.Token); diff --git a/UnitTests/ScenarioTests.cs b/UnitTests/ScenarioTests.cs index 10757b366..751694811 100644 --- a/UnitTests/ScenarioTests.cs +++ b/UnitTests/ScenarioTests.cs @@ -48,15 +48,16 @@ namespace Terminal.Gui { List scenarioClasses = Scenario.GetDerivedClasses (); Assert.NotEmpty (scenarioClasses); - lock (FakeConsole.MockKeyPresses) { - foreach (var scenarioClass in scenarioClasses) { + foreach (var scenarioClass in scenarioClasses) { - // Setup some fake keypresses - // Passing empty string will cause just a ctrl-q to be fired - FakeConsole.MockKeyPresses.Clear (); - int stackSize = CreateInput (""); + // Setup some fake keypresses + // Passing empty string will cause just a ctrl-q to be fired + FakeConsole.MockKeyPresses.Clear (); + int stackSize = CreateInput (""); - Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true))); + Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true))); + + lock (FakeConsole.MockKeyPresses) { int iterations = 0; Application.Iteration = () => { @@ -96,6 +97,8 @@ namespace Terminal.Gui { if (abortCount != 0) { output.WriteLine ($"Scenario {scenarioClass} had abort count of {abortCount}"); + } else if (iterations > 1) { + output.WriteLine ($"Scenario {scenarioClass} had iterations count of {iterations}"); } Assert.Equal (0, abortCount);