From e3f5b8f83cc3bdf19ed3819918b0395a90485609 Mon Sep 17 00:00:00 2001 From: Brandon Thetford Date: Wed, 21 Feb 2024 03:28:07 -0700 Subject: [PATCH] Move this to its own file (it's still a nested class though) --- .../Application.MainLoopSyncContext.cs | 51 +++++++++++++++++++ Terminal.Gui/Application.cs | 47 ----------------- 2 files changed, 51 insertions(+), 47 deletions(-) create mode 100644 Terminal.Gui/Application.MainLoopSyncContext.cs diff --git a/Terminal.Gui/Application.MainLoopSyncContext.cs b/Terminal.Gui/Application.MainLoopSyncContext.cs new file mode 100644 index 000000000..dfda0727a --- /dev/null +++ b/Terminal.Gui/Application.MainLoopSyncContext.cs @@ -0,0 +1,51 @@ +namespace Terminal.Gui; + +public static partial class Application +{ + /// + /// provides the sync context set while executing code in Terminal.Gui, to let + /// users use async/await on their code + /// + private sealed class MainLoopSyncContext : SynchronizationContext + { + public override SynchronizationContext CreateCopy () { return new MainLoopSyncContext (); } + + public override void Post (SendOrPostCallback d, object state) + { + MainLoop.AddIdle ( + () => + { + d (state); + + return false; + } + ); + } + + //_mainLoop.Driver.Wakeup (); + public override void Send (SendOrPostCallback d, object state) + { + if (Thread.CurrentThread.ManagedThreadId == _mainThreadId) + { + d (state); + } + else + { + var wasExecuted = false; + + Invoke ( + () => + { + d (state); + wasExecuted = true; + } + ); + + while (!wasExecuted) + { + Thread.Sleep (15); + } + } + } + } +} diff --git a/Terminal.Gui/Application.cs b/Terminal.Gui/Application.cs index fd17d966d..9c402e76c 100644 --- a/Terminal.Gui/Application.cs +++ b/Terminal.Gui/Application.cs @@ -716,53 +716,6 @@ public static partial class Application /// public static bool EndAfterFirstIteration { get; set; } - // - // provides the sync context set while executing code in Terminal.Gui, to let - // users use async/await on their code - // - private sealed class MainLoopSyncContext : SynchronizationContext - { - public override SynchronizationContext CreateCopy () { return new MainLoopSyncContext (); } - - public override void Post (SendOrPostCallback d, object state) - { - MainLoop.AddIdle ( - () => - { - d (state); - - return false; - } - ); - } - - //_mainLoop.Driver.Wakeup (); - public override void Send (SendOrPostCallback d, object state) - { - if (Thread.CurrentThread.ManagedThreadId == _mainThreadId) - { - d (state); - } - else - { - var wasExecuted = false; - - Invoke ( - () => - { - d (state); - wasExecuted = true; - } - ); - - while (!wasExecuted) - { - Thread.Sleep (15); - } - } - } - } - /// Building block API: Runs the main loop for the created . /// The state returned by the method. public static void RunLoop (RunState state)