2
0
BDisp 5 жил өмнө
parent
commit
997a7c31b5

+ 25 - 18
Terminal.Gui/Drivers/WindowsDriver.cs

@@ -522,9 +522,9 @@ namespace Terminal.Gui {
 		void WindowsInputHandler ()
 		{
 			while (true) {
-				waitForProbe.Wait ();				
+				waitForProbe.Wait ();
 				waitForProbe.Reset ();
-				
+
 				uint numberEventsRead = 0;
 
 				WindowsConsole.ReadConsoleInput (winConsole.InputHandle, records, 1, out numberEventsRead);
@@ -551,33 +551,22 @@ namespace Terminal.Gui {
 
 		bool IMainLoopDriver.EventsPending (bool wait)
 		{
-			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
-				waitTimeout = -1;
+			int waitTimeout = 0;
 
-			if (!wait)
-				waitTimeout = 0;
+			if (CkeckTimeout (wait, ref waitTimeout))
+				return true;
 
 			result = null;
 			waitForProbe.Set ();
 
 			try {
 				while (result == null) {
-					if (wait && waitTimeout == -1) {
-						waitTimeout = 0;
-					}
 					if (!tokenSource.IsCancellationRequested)
-						eventReady.Wait (waitTimeout, tokenSource.Token);
+						eventReady.Wait (0, tokenSource.Token);
 					if (result != null) {
 						break;
 					}
-					if (mainLoop.timeouts.Count > 0 || mainLoop.idleHandlers.Count > 0) {
+					if (mainLoop.idleHandlers.Count > 0 || CkeckTimeout (wait, ref waitTimeout)) {
 						return true;
 					}
 				}
@@ -595,6 +584,24 @@ namespace Terminal.Gui {
 			return true;
 		}
 
+		bool CkeckTimeout (bool wait, ref int waitTimeout)
+		{
+			long now = DateTime.UtcNow.Ticks;
+
+			if (mainLoop.timeouts.Count > 0) {
+				waitTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
+				if (waitTimeout < 0)
+					return true;
+			} else {
+				waitTimeout = -1;
+			}
+
+			if (!wait)
+				waitTimeout = 0;
+
+			return false;
+		}
+
 		Action<KeyEvent> keyHandler;
 		Action<KeyEvent> keyDownHandler;
 		Action<KeyEvent> keyUpHandler;

+ 24 - 17
Terminal.Gui/MonoCurses/mainloop.cs

@@ -198,31 +198,20 @@ namespace Mono.Terminal {
 
 		bool IMainLoopDriver.EventsPending (bool wait)
 		{
-			long now = DateTime.UtcNow.Ticks;
-
-			int pollTimeout, n;
-			if (mainLoop.timeouts.Count > 0) {
-				pollTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
-				if (pollTimeout < 0)
-					return true;
+			int pollTimeout = 0;
+			int n;
 
-			} else
-				pollTimeout = -1;
-
-			if (!wait)
-				pollTimeout = 0;
+			if (CkeckTimeout (wait, ref pollTimeout))
+				return true;
 
 			UpdatePollMap ();
 
 			while (true) {
-				if (wait && pollTimeout == -1) {
-					pollTimeout = 0;
-				}
-				n = poll (pollmap, (uint)pollmap.Length, pollTimeout);
+				n = poll (pollmap, (uint)pollmap.Length, 0);
 				if (pollmap != null) {
 					break;
 				}
-				if (mainLoop.timeouts.Count > 0 || mainLoop.idleHandlers.Count > 0) {
+				if (mainLoop.idleHandlers.Count > 0 || CkeckTimeout (wait, ref pollTimeout)) {
 					return true;
 				}
 			}
@@ -232,6 +221,24 @@ namespace Mono.Terminal {
 			return n > 0 || mainLoop.timeouts.Count > 0 && ((mainLoop.timeouts.Keys [0] - DateTime.UtcNow.Ticks) < 0) || ic > 0;
 		}
 
+		bool CkeckTimeout (bool wait, ref int pollTimeout)
+		{
+			long now = DateTime.UtcNow.Ticks;
+
+			if (mainLoop.timeouts.Count > 0) {
+				pollTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
+				if (pollTimeout < 0)
+					return true;
+
+			} else
+				pollTimeout = -1;
+
+			if (!wait)
+				pollTimeout = 0;
+
+			return false;
+		}
+
 		void IMainLoopDriver.MainIteration ()
 		{
 			if (pollmap != null) {