mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2026-01-01 16:59:35 +01:00
Move this to its own file (it's still a nested class though)
This commit is contained in:
51
Terminal.Gui/Application.MainLoopSyncContext.cs
Normal file
51
Terminal.Gui/Application.MainLoopSyncContext.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
namespace Terminal.Gui;
|
||||
|
||||
public static partial class Application
|
||||
{
|
||||
/// <summary>
|
||||
/// provides the sync context set while executing code in Terminal.Gui, to let
|
||||
/// users use async/await on their code
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user