Browse Source

[Re]move unwanted allocation (new CancellationTokenSource()) in WindowsDriver.EventsPending to only when needed (#274)

giladlevi 5 years ago
parent
commit
f0a079b242
1 changed files with 8 additions and 3 deletions
  1. 8 3
      Terminal.Gui/Drivers/WindowsDriver.cs

+ 8 - 3
Terminal.Gui/Drivers/WindowsDriver.cs

@@ -503,8 +503,7 @@ namespace Terminal.Gui {
 
 			result = null;
 			waitForProbe.Set();
-			tokenSource.Dispose();
-			tokenSource = new CancellationTokenSource();
+
 			try {
 				eventReady.Wait(waitTimeout, tokenSource.Token);
 			} catch (OperationCanceledException) {
@@ -513,7 +512,13 @@ namespace Terminal.Gui {
 				eventReady.Reset();
 			}
 			Debug.WriteLine("Events ready");
-			return result != null || tokenSource.IsCancellationRequested;
+
+			if (!tokenSource.IsCancellationRequested)
+				return result != null;
+
+			tokenSource.Dispose();
+			tokenSource = new CancellationTokenSource();
+			return true;
 		}
 
 		Action<KeyEvent> keyHandler;