浏览代码

Merge branch 'v2_develop' into v2_menu-can-execute-false-fix_2787

Tig 1 年之前
父节点
当前提交
78acec79cf
共有 2 个文件被更改,包括 24 次插入13 次删除
  1. 15 4
      Terminal.Gui/ConsoleDrivers/CursesDriver/UnixMainLoop.cs
  2. 9 9
      UICatalog/Scenarios/Threading.cs

+ 15 - 4
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;
 
@@ -101,11 +100,11 @@ namespace Terminal.Gui {
 			if (ConsoleDriver.RunningUnitTests) {
 				return;
 			}
-			
+
 			try {
 				pipe (wakeupPipes);
-				AddWatch (wakeupPipes [1], Condition.PollIn, ml => {
-					read (wakeupPipes [1], ignore, readHandle);
+				AddWatch (wakeupPipes [0], Condition.PollIn, ml => {
+					read (wakeupPipes [0], ignore, (IntPtr)1);
 					return true;
 				});
 			} catch (DllNotFoundException e) {
@@ -190,6 +189,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

+ 9 - 9
UICatalog/Scenarios/Threading.cs

@@ -45,7 +45,7 @@ namespace UICatalog.Scenarios {
 			};
 
 			_btnActionCancel = new Button (1, 1, "Cancelable Load Items");
-			_btnActionCancel.Clicked += (s,e) => Application.MainLoop.Invoke (CallLoadItemsAsync);
+			_btnActionCancel.Clicked += (s, e) => Application.MainLoop.Invoke (CallLoadItemsAsync);
 
 			Win.Add (new Label ("Data Items:") {
 				X = Pos.X (_btnActionCancel),
@@ -76,19 +76,19 @@ namespace UICatalog.Scenarios {
 			var text = new TextField (1, 3, 100, "Type anything after press the button");
 
 			var _btnAction = new Button (80, 10, "Load Data Action");
-			_btnAction.Clicked += (s,e) => _action.Invoke ();
+			_btnAction.Clicked += (s, e) => _action.Invoke ();
 			var _btnLambda = new Button (80, 12, "Load Data Lambda");
-			_btnLambda.Clicked += (s,e) => _lambda.Invoke ();
+			_btnLambda.Clicked += (s, e) => _lambda.Invoke ();
 			var _btnHandler = new Button (80, 14, "Load Data Handler");
-			_btnHandler.Clicked += (s,e) => _handler.Invoke (null, new EventArgs ());
+			_btnHandler.Clicked += (s, e) => _handler.Invoke (null, new EventArgs ());
 			var _btnSync = new Button (80, 16, "Load Data Synchronous");
-			_btnSync.Clicked += (s,e) => _sync.Invoke ();
+			_btnSync.Clicked += (s, e) => _sync.Invoke ();
 			var _btnMethod = new Button (80, 18, "Load Data Method");
-			_btnMethod.Clicked += async (s,e) => await MethodAsync ();
+			_btnMethod.Clicked += async (s, e) => await MethodAsync ();
 			var _btnClearData = new Button (80, 20, "Clear Data");
-			_btnClearData.Clicked += (s,e) => { _itemsList.Source = null; LogJob ("Cleaning Data"); };
+			_btnClearData.Clicked += (s, e) => { _itemsList.Source = null; LogJob ("Cleaning Data"); };
 			var _btnQuit = new Button (80, 22, "Quit");
-			_btnQuit.Clicked += (s,e) => Application.RequestStop ();
+			_btnQuit.Clicked += (s, e) => Application.RequestStop ();
 
 			Win.Add (_itemsList, _btnActionCancel, _logJob, text, _btnAction, _btnLambda, _btnHandler, _btnSync, _btnMethod, _btnClearData, _btnQuit);
 
@@ -156,7 +156,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!");
 				}