Browse Source

Merge pull request #27 from BDisp/tig_v2_3338-Toplevel-Must-Be-Disposed

Set Visible to false on Closing event.
Tig 1 year ago
parent
commit
d3599c9daf

+ 1 - 1
Terminal.Gui/Application.MainLoopSyncContext.cs

@@ -12,7 +12,7 @@ public static partial class Application
 
 
         public override void Post (SendOrPostCallback d, object state)
         public override void Post (SendOrPostCallback d, object state)
         {
         {
-            MainLoop.AddIdle (
+            MainLoop?.AddIdle (
                               () =>
                               () =>
                               {
                               {
                                   d (state);
                                   d (state);

+ 15 - 1
Terminal.Gui/Application.cs

@@ -676,6 +676,15 @@ public static partial class Application
             // by using NotifyStopRunState event.
             // by using NotifyStopRunState event.
             RunLoop (runState);
             RunLoop (runState);
 
 
+            if (runState.Toplevel is null)
+            {
+#if DEBUG_IDISPOSABLE
+                Debug.Assert (_topLevels.Count == 0);
+#endif
+                runState.Dispose ();
+                return;
+            }
+
             if (!EndAfterFirstIteration)
             if (!EndAfterFirstIteration)
             {
             {
                 End (runState);
                 End (runState);
@@ -787,7 +796,7 @@ public static partial class Application
 
 
         var firstIteration = true;
         var firstIteration = true;
 
 
-        for (state.Toplevel.Running = true; state.Toplevel.Running;)
+        for (state.Toplevel.Running = true; state.Toplevel?.Running == true;)
         {
         {
             MainLoop.Running = true;
             MainLoop.Running = true;
 
 
@@ -838,6 +847,11 @@ public static partial class Application
 
 
         firstIteration = false;
         firstIteration = false;
 
 
+        if (Current == null)
+        {
+            return;
+        }
+
         if (state.Toplevel != Top && (Top.NeedsDisplay || Top.SubViewNeedsDisplay || Top.LayoutNeeded))
         if (state.Toplevel != Top && (Top.NeedsDisplay || Top.SubViewNeedsDisplay || Top.LayoutNeeded))
         {
         {
             state.Toplevel.SetNeedsDisplay (state.Toplevel.Frame);
             state.Toplevel.SetNeedsDisplay (state.Toplevel.Frame);

+ 19 - 1
UICatalog/Scenarios/BackgroundWorkerCollection.cs

@@ -1,6 +1,7 @@
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.ComponentModel;
+using System.Diagnostics;
 using System.Threading;
 using System.Threading;
 using Terminal.Gui;
 using Terminal.Gui;
 
 
@@ -19,6 +20,10 @@ public class BackgroundWorkerCollection : Scenario
 
 
         //main.Dispose ();
         //main.Dispose ();
         Application.Top.Dispose ();
         Application.Top.Dispose ();
+
+#if DEBUG_IDISPOSABLE
+        Debug.Assert (Application.OverlappedChildren.Count == 0);
+#endif
     }
     }
 
 
     public override void Run () { }
     public override void Run () { }
@@ -182,6 +187,7 @@ public class BackgroundWorkerCollection : Scenario
         {
         {
             _workerApp?.WriteLog ($"{top.Toplevel.Data} deactivate.");
             _workerApp?.WriteLog ($"{top.Toplevel.Data} deactivate.");
         }
         }
+
         private void Quit () { RequestStop (); }
         private void Quit () { RequestStop (); }
 
 
         private MenuBarItem View ()
         private MenuBarItem View ()
@@ -356,6 +362,12 @@ public class BackgroundWorkerCollection : Scenario
             Add (_listLog);
             Add (_listLog);
 
 
             Closing += WorkerApp_Closing;
             Closing += WorkerApp_Closing;
+            Closed += WorkerApp_Closed;
+        }
+
+        private void WorkerApp_Closed (object sender, ToplevelEventArgs e)
+        {
+            CancelWorker ();
         }
         }
         private void WorkerApp_Closing (object sender, ToplevelClosingEventArgs e)
         private void WorkerApp_Closing (object sender, ToplevelClosingEventArgs e)
         {
         {
@@ -474,7 +486,13 @@ public class BackgroundWorkerCollection : Scenario
 
 
                                                  _stagingsUi.Add (stagingUI);
                                                  _stagingsUi.Add (stagingUI);
                                                  _stagingWorkers.Remove (staging);
                                                  _stagingWorkers.Remove (staging);
-
+#if DEBUG_IDISPOSABLE
+                                                 if (Application.OverlappedTop is null)
+                                                 {
+                                                     stagingUI.Dispose ();
+                                                     return;
+                                                 }
+#endif
                                                  Application.Run (stagingUI);
                                                  Application.Run (stagingUI);
                                                  stagingUI.Dispose ();
                                                  stagingUI.Dispose ();
                                              }
                                              }