From add2877e09a2145c75c2e316c445c5c9fc0e7e89 Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 24 Jul 2025 13:26:51 +0100 Subject: [PATCH] Fixes #4206. There is no need to use a Task.Run to force Invoke in the MainThreadId (#4207) --- Terminal.Gui/App/Application.Run.cs | 12 ++---------- Terminal.Gui/App/ApplicationImpl.cs | 13 ++++++++++--- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Terminal.Gui/App/Application.Run.cs b/Terminal.Gui/App/Application.Run.cs index 443e97ade..30c1384a3 100644 --- a/Terminal.Gui/App/Application.Run.cs +++ b/Terminal.Gui/App/Application.Run.cs @@ -212,16 +212,8 @@ public static partial class Application // Run (Begin, Run, End, Stop) NotifyNewRunState?.Invoke (toplevel, new (rs)); - if (!ConsoleDriver.RunningUnitTests) - { - // Force an Idle event to be added to timeout outside the Application.MainThreadId, - // so that an Iteration (and Refresh) happen in the Application.MainThreadId - Task.Run (() => - { - Invoke (() => { }); - Task.Delay (1).Wait (); - }); - } + // Force an Idle event so that an Iteration (and Refresh) happen. + Invoke (() => { }); return rs; } diff --git a/Terminal.Gui/App/ApplicationImpl.cs b/Terminal.Gui/App/ApplicationImpl.cs index 01465e6d1..d3b7fed57 100644 --- a/Terminal.Gui/App/ApplicationImpl.cs +++ b/Terminal.Gui/App/ApplicationImpl.cs @@ -275,6 +275,8 @@ public class ApplicationImpl : IApplication if (Application.MainThreadId == Thread.CurrentThread.ManagedThreadId) { action (); + WakeupMainLoop (); + return; } @@ -294,9 +296,14 @@ public class ApplicationImpl : IApplication } ); - // Ensure the action is executed in the main loop - // Wakeup mainloop if it's waiting for events - Application.MainLoop.Wakeup (); + WakeupMainLoop (); + + void WakeupMainLoop () + { + // Ensure the action is executed in the main loop + // Wakeup mainloop if it's waiting for events + Application.MainLoop?.Wakeup (); + } } ///