From 4f683d33b1743a4def86294ead5c80c834a9cbe4 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 29 Aug 2024 18:42:55 -0600 Subject: [PATCH] Added stopwatch --- .../Application/Application.Initialization.cs | 10 +++++-- UICatalog/UICatalog.cs | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/Application/Application.Initialization.cs b/Terminal.Gui/Application/Application.Initialization.cs index d9b4529d0..6c3a70a02 100644 --- a/Terminal.Gui/Application/Application.Initialization.cs +++ b/Terminal.Gui/Application/Application.Initialization.cs @@ -198,10 +198,16 @@ public static partial class Application // Initialization (Init/Shutdown) public static void Shutdown () { // TODO: Throw an exception if Init hasn't been called. + + bool wasInitialized = IsInitialized; ResetState (); PrintJsonErrors (); - bool init = IsInitialized; - InitializedChanged?.Invoke (null, new (in init)); + + if (wasInitialized) + { + bool init = IsInitialized; + InitializedChanged?.Invoke (null, new (in init)); + } } /// diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index a6f90edfe..4768c285d 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -13,6 +13,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Text.Json.Serialization; using Terminal.Gui; +using UICatalog.Scenarios; using static Terminal.Gui.ConfigurationManager; using Command = Terminal.Gui.Command; using RuntimeEnvironment = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment; @@ -337,6 +338,14 @@ public class UICatalogApp Apply (); scenario.Theme = _cachedTheme; scenario.TopLevelColorScheme = _topLevelColorScheme; + +#if DEBUG_IDISPOSABLE + // Measure how long it takes for the app to shut down + var sw = new Stopwatch (); + string scenarioName = scenario.GetName (); + Application.InitializedChanged += ApplicationOnInitializedChanged; +#endif + scenario.Main (); scenario.Dispose (); @@ -345,10 +354,31 @@ public class UICatalogApp // TODO: Throw if shutdown was not called already Application.Shutdown (); VerifyObjectsWereDisposed (); + +#if DEBUG_IDISPOSABLE + Application.InitializedChanged -= ApplicationOnInitializedChanged; + + void ApplicationOnInitializedChanged (object? sender, EventArgs e) + { + if (e.CurrentValue) + { + sw.Start (); + } + else + { + sw.Stop (); + Debug.WriteLine ($"Shutdown of {scenarioName} Scenario took {sw.ElapsedMilliseconds}ms"); + } + } +#endif } StopConfigFileWatcher (); VerifyObjectsWereDisposed (); + + return; + + } private static void VerifyObjectsWereDisposed ()