From add31bd83a977a33b66dc7eeb5e9764575c77ce9 Mon Sep 17 00:00:00 2001 From: Szymon Pobiega Date: Mon, 13 May 2019 04:01:32 +0200 Subject: [PATCH] Implement WakeUp in WindowsDriver (#208) --- Terminal.Gui/Drivers/WindowsDriver.cs | 28 ++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/Terminal.Gui/Drivers/WindowsDriver.cs b/Terminal.Gui/Drivers/WindowsDriver.cs index 68bf5cf83..82a20374b 100644 --- a/Terminal.Gui/Drivers/WindowsDriver.cs +++ b/Terminal.Gui/Drivers/WindowsDriver.cs @@ -26,6 +26,7 @@ // SOFTWARE. // using System; +using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -414,8 +415,8 @@ namespace Terminal.Gui { internal class WindowsDriver : ConsoleDriver, Mono.Terminal.IMainLoopDriver { static bool sync; - AutoResetEvent eventReady = new AutoResetEvent (false); - AutoResetEvent waitForProbe = new AutoResetEvent (false); + ManualResetEventSlim eventReady = new ManualResetEventSlim(false); + ManualResetEventSlim waitForProbe = new ManualResetEventSlim(false); MainLoop mainLoop; Action TerminalResized; WindowsConsole.CharInfo [] OutputBuffer; @@ -460,7 +461,8 @@ namespace Terminal.Gui { void WindowsInputHandler () { while (true) { - waitForProbe.WaitOne (); + waitForProbe.Wait(); + waitForProbe.Reset(); uint numberEventsRead = 0; @@ -470,7 +472,7 @@ namespace Terminal.Gui { else result = records; - eventReady.Set (); + eventReady.Set(); } } @@ -481,6 +483,7 @@ namespace Terminal.Gui { void IMainLoopDriver.Wakeup () { + tokenSource.Cancel(); } bool IMainLoopDriver.EventsPending (bool wait) @@ -499,9 +502,18 @@ namespace Terminal.Gui { waitTimeout = 0; result = null; - waitForProbe.Set (); - eventReady.WaitOne (waitTimeout); - return result != null; + waitForProbe.Set(); + tokenSource.Dispose(); + tokenSource = new CancellationTokenSource(); + try { + eventReady.Wait(waitTimeout, tokenSource.Token); + } catch (OperationCanceledException) { + return true; + } finally { + eventReady.Reset(); + } + Debug.WriteLine("Events ready"); + return result != null || tokenSource.IsCancellationRequested; } Action keyHandler; @@ -811,6 +823,8 @@ namespace Terminal.Gui { } int currentAttribute; + CancellationTokenSource tokenSource = new CancellationTokenSource(); + public override void SetAttribute (Attribute c) { currentAttribute = c.value;