diff --git a/Terminal.Gui/Application.MainLoopSyncContext.cs b/Terminal.Gui/Application.MainLoopSyncContext.cs index dfda0727ae..513608e8c1 100644 --- a/Terminal.Gui/Application.MainLoopSyncContext.cs +++ b/Terminal.Gui/Application.MainLoopSyncContext.cs @@ -12,7 +12,7 @@ private sealed class MainLoopSyncContext : SynchronizationContext public override void Post (SendOrPostCallback d, object state) { - MainLoop.AddIdle ( + MainLoop?.AddIdle ( () => { d (state); diff --git a/Terminal.Gui/Application.cs b/Terminal.Gui/Application.cs index 2bde0bca09..2e6004971b 100644 --- a/Terminal.Gui/Application.cs +++ b/Terminal.Gui/Application.cs @@ -676,6 +676,15 @@ public static void Run (Toplevel view, Func errorHandler = null // by using NotifyStopRunState event. RunLoop (runState); + if (runState.Toplevel is null) + { +#if DEBUG_IDISPOSABLE + Debug.Assert (_topLevels.Count == 0); +#endif + runState.Dispose (); + return; + } + if (!EndAfterFirstIteration) { End (runState); @@ -787,7 +796,7 @@ public static void RunLoop (RunState state) var firstIteration = true; - for (state.Toplevel.Running = true; state.Toplevel.Running;) + for (state.Toplevel.Running = true; state.Toplevel?.Running == true;) { MainLoop.Running = true; @@ -838,6 +847,11 @@ public static void RunIteration (ref RunState state, ref bool firstIteration) firstIteration = false; + if (Current == null) + { + return; + } + if (state.Toplevel != Top && (Top.NeedsDisplay || Top.SubViewNeedsDisplay || Top.LayoutNeeded)) { state.Toplevel.SetNeedsDisplay (state.Toplevel.Frame); diff --git a/UICatalog/Scenarios/BackgroundWorkerCollection.cs b/UICatalog/Scenarios/BackgroundWorkerCollection.cs index b2a78aea19..214d453da2 100644 --- a/UICatalog/Scenarios/BackgroundWorkerCollection.cs +++ b/UICatalog/Scenarios/BackgroundWorkerCollection.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics; using System.Threading; using Terminal.Gui; @@ -19,6 +20,10 @@ public override void Init () //main.Dispose (); Application.Top.Dispose (); + +#if DEBUG_IDISPOSABLE + Debug.Assert (Application.OverlappedChildren.Count == 0); +#endif } public override void Run () { } @@ -182,6 +187,7 @@ private void OverlappedMain_Deactivate (object sender, ToplevelEventArgs top) { _workerApp?.WriteLog ($"{top.Toplevel.Data} deactivate."); } + private void Quit () { RequestStop (); } private MenuBarItem View () @@ -356,6 +362,12 @@ public WorkerApp () Add (_listLog); Closing += WorkerApp_Closing; + Closed += WorkerApp_Closed; + } + + private void WorkerApp_Closed (object sender, ToplevelEventArgs e) + { + CancelWorker (); } private void WorkerApp_Closing (object sender, ToplevelClosingEventArgs e) { @@ -474,7 +486,13 @@ public void RunWorker () _stagingsUi.Add (stagingUI); _stagingWorkers.Remove (staging); - +#if DEBUG_IDISPOSABLE + if (Application.OverlappedTop is null) + { + stagingUI.Dispose (); + return; + } +#endif Application.Run (stagingUI); stagingUI.Dispose (); }