Browse Source

Merge pull request #2848 from BDisp/v1_cursesdriver-threading-fix_2847

Fixes #2847. CursesDriver doesn't processing response from task threading.
Tig 1 year ago
parent
commit
b37bdcd860

+ 1 - 0
Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

@@ -142,6 +142,7 @@ namespace Terminal.Gui {
 			Curses.raw ();
 			Curses.noecho ();
 			Curses.refresh ();
+			ProcessWinChange ();
 		}
 
 		private void ProcessWinChange ()

+ 14 - 3
Terminal.Gui/ConsoleDrivers/CursesDriver/UnixMainLoop.cs

@@ -82,7 +82,6 @@ namespace Terminal.Gui {
 		bool poll_dirty = true;
 		int [] wakeupPipes = new int [2];
 		static IntPtr ignore = Marshal.AllocHGlobal (1);
-		static IntPtr readHandle = Marshal.AllocHGlobal (1);
 		MainLoop mainLoop;
 		bool winChanged;
 
@@ -97,8 +96,8 @@ namespace Terminal.Gui {
 		{
 			this.mainLoop = mainLoop;
 			pipe (wakeupPipes);
-			AddWatch (wakeupPipes [1], Condition.PollIn, ml => {
-				read (wakeupPipes [1], ignore, readHandle);
+			AddWatch (wakeupPipes [0], Condition.PollIn, ml => {
+				read (wakeupPipes [1], ignore, (IntPtr)1);
 				return true;
 			});
 		}
@@ -176,6 +175,18 @@ namespace Terminal.Gui {
 			if (mainLoop.timeouts.Count > 0) {
 				pollTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
 				if (pollTimeout < 0) {
+					// This avoids 'poll' waiting infinitely if 'pollTimeout < 0' until some action is detected
+					// This can occur after IMainLoopDriver.Wakeup is executed where the pollTimeout is less than 0
+					// and no event occurred in elapsed time when the 'poll' is start running again.
+					/*
+					The 'poll' function in the C standard library uses a signed integer as the timeout argument, where:
+
+					    - A positive value specifies a timeout in milliseconds.
+					    - A value of 0 means the poll function will return immediately, checking for events and not waiting.
+					    - A value of -1 means the poll function will wait indefinitely until an event occurs or an error occurs.
+					    - A negative value other than -1 typically indicates an error.
+					 */
+					pollTimeout = 0;
 					return true;
 				}
 			} else

+ 1 - 1
UICatalog/Scenarios/Threading.cs

@@ -157,7 +157,7 @@ namespace UICatalog.Scenarios {
 					LogJob ($"Returned from task Thread:{Thread.CurrentThread.ManagedThreadId} {DateTime.Now}");
 					_itemsList.SetSource (items);
 					LogJob ($"Finished populate list view Thread:{Thread.CurrentThread.ManagedThreadId} {DateTime.Now}");
-					_btnActionCancel.Text = "Load Items";
+					_btnActionCancel.Text = "Cancelable Load Items";
 				} else {
 					LogJob ("Task was canceled!");
 				}