mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
Fixes #4208. MainLoopSyncContext doesn't work with the v2 drivers
This commit is contained in:
@@ -11,15 +11,22 @@ internal sealed class MainLoopSyncContext : SynchronizationContext
|
||||
public override void Post (SendOrPostCallback d, object state)
|
||||
{
|
||||
// Queue the task
|
||||
Application.MainLoop?.TimedEvents.Add (TimeSpan.Zero,
|
||||
() =>
|
||||
{
|
||||
d (state);
|
||||
if (ApplicationImpl.Instance.IsLegacy)
|
||||
{
|
||||
Application.MainLoop?.TimedEvents.Add (TimeSpan.Zero,
|
||||
() =>
|
||||
{
|
||||
d (state);
|
||||
|
||||
return false;
|
||||
}
|
||||
);
|
||||
Application.MainLoop?.Wakeup ();
|
||||
return false;
|
||||
}
|
||||
);
|
||||
Application.MainLoop?.Wakeup ();
|
||||
}
|
||||
else
|
||||
{
|
||||
ApplicationImpl.Instance.Invoke (() => { d (state); });
|
||||
}
|
||||
}
|
||||
|
||||
//_mainLoop.Driver.Wakeup ();
|
||||
|
||||
@@ -21,43 +21,73 @@ public class SyncrhonizationContextTests
|
||||
Application.Shutdown ();
|
||||
}
|
||||
|
||||
private object _lockPost = new ();
|
||||
|
||||
[Theory]
|
||||
[InlineData (typeof (FakeDriver))]
|
||||
[InlineData (typeof (NetDriver))]
|
||||
[InlineData (typeof (WindowsDriver))]
|
||||
[InlineData (typeof (CursesDriver))]
|
||||
public void SynchronizationContext_Post (Type driverType)
|
||||
[InlineData (typeof (ConsoleDriverFacade<WindowsConsole.InputRecord>), "v2win")]
|
||||
[InlineData (typeof (ConsoleDriverFacade<ConsoleKeyInfo>), "v2net")]
|
||||
public void SynchronizationContext_Post (Type driverType, string driverName = null)
|
||||
{
|
||||
ConsoleDriver.RunningUnitTests = true;
|
||||
Application.Init (driverName: driverType.Name);
|
||||
SynchronizationContext context = SynchronizationContext.Current;
|
||||
lock (_lockPost)
|
||||
{
|
||||
ConsoleDriver.RunningUnitTests = true;
|
||||
|
||||
var success = false;
|
||||
if (driverType.Name.Contains ("ConsoleDriverFacade"))
|
||||
{
|
||||
Application.Init (driverName: driverName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Application.Init (driverName: driverType.Name);
|
||||
}
|
||||
|
||||
Task.Run (
|
||||
() =>
|
||||
{
|
||||
Thread.Sleep (500);
|
||||
SynchronizationContext context = SynchronizationContext.Current;
|
||||
|
||||
// non blocking
|
||||
context.Post (
|
||||
delegate
|
||||
{
|
||||
success = true;
|
||||
var success = false;
|
||||
|
||||
// then tell the application to quit
|
||||
Application.Invoke (() => Application.RequestStop ());
|
||||
},
|
||||
null
|
||||
);
|
||||
Assert.False (success);
|
||||
}
|
||||
);
|
||||
Task.Run (() =>
|
||||
{
|
||||
while (Application.Top is null || Application.Top is { Running: false })
|
||||
{
|
||||
Thread.Sleep (500);
|
||||
}
|
||||
|
||||
// blocks here until the RequestStop is processed at the end of the test
|
||||
Application.Run ().Dispose ();
|
||||
Assert.True (success);
|
||||
Application.Shutdown ();
|
||||
// non blocking
|
||||
context.Post (
|
||||
delegate
|
||||
{
|
||||
success = true;
|
||||
|
||||
// then tell the application to quit
|
||||
Application.Invoke (() => Application.RequestStop ());
|
||||
},
|
||||
null
|
||||
);
|
||||
|
||||
if (Application.Top is { Running: true })
|
||||
{
|
||||
Assert.False (success);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// blocks here until the RequestStop is processed at the end of the test
|
||||
Application.Run ().Dispose ();
|
||||
Assert.True (success);
|
||||
|
||||
if (ApplicationImpl.Instance is ApplicationV2)
|
||||
{
|
||||
ApplicationImpl.Instance.Shutdown ();
|
||||
}
|
||||
else
|
||||
{
|
||||
Application.Shutdown ();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Reference in New Issue
Block a user