From a40e90f06e45ecd2c29e7291204474fc9b577124 Mon Sep 17 00:00:00 2001 From: Tig Date: Tue, 21 Oct 2025 01:49:58 -0600 Subject: [PATCH] Refactor Scrolling.cs for timer management and nullability Enabled nullable reference types with `#nullable enable` for improved safety. Replaced the `pulsing` flag with a new `_progressTimer` object to better manage the progress bar's timer lifecycle. Updated `AppOnInitialized` and `AppUnloaded` methods to handle timer initialization and cleanup properly, preventing potential memory leaks. Simplified code by removing unnecessary comments and aligning method signatures with nullable reference type annotations. --- Examples/UICatalog/Scenarios/Scrolling.cs | 32 ++++++++++++----------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/Examples/UICatalog/Scenarios/Scrolling.cs b/Examples/UICatalog/Scenarios/Scrolling.cs index 6b14d755b..43776fa83 100644 --- a/Examples/UICatalog/Scenarios/Scrolling.cs +++ b/Examples/UICatalog/Scenarios/Scrolling.cs @@ -1,4 +1,6 @@ -namespace UICatalog.Scenarios; +#nullable enable + +namespace UICatalog.Scenarios; [ScenarioMetadata ("Scrolling", "Content scrolling, IScrollBars, etc...")] [ScenarioCategory ("Controls")] @@ -6,6 +8,8 @@ [ScenarioCategory ("Tests")] public class Scrolling : Scenario { + private static object? _progressTimer = null; + public override void Main () { Application.Init (); @@ -38,10 +42,6 @@ public class Scrolling : Scenario app.Add (demoView); - //// NOTE: This call to EnableScrollBar is technically not needed because the reference - //// NOTE: to demoView.HorizontalScrollBar below will cause it to be lazy created. - //// NOTE: The call included in this sample to for illustration purposes. - //demoView.EnableScrollBar (Orientation.Horizontal); var hCheckBox = new CheckBox { X = Pos.X (demoView), @@ -52,10 +52,6 @@ public class Scrolling : Scenario app.Add (hCheckBox); hCheckBox.CheckedStateChanged += (sender, args) => { demoView.HorizontalScrollBar.Visible = args.Value == CheckState.Checked; }; - //// NOTE: This call to EnableScrollBar is technically not needed because the reference - //// NOTE: to demoView.HorizontalScrollBar below will cause it to be lazy created. - //// NOTE: The call included in this sample to for illustration purposes. - //demoView.EnableScrollBar (Orientation.Vertical); var vCheckBox = new CheckBox { X = Pos.Right (hCheckBox) + 3, @@ -96,8 +92,6 @@ public class Scrolling : Scenario app.Add (progress); - var pulsing = true; - app.Initialized += AppOnInitialized; app.Unloaded += AppUnloaded; @@ -108,17 +102,25 @@ public class Scrolling : Scenario return; - void AppOnInitialized (object sender, EventArgs e) + void AppOnInitialized (object? sender, EventArgs e) { bool TimerFn () { progress.Pulse (); - return pulsing; + return _progressTimer is { }; } - Application.AddTimeout (TimeSpan.FromMilliseconds (200), TimerFn); + _progressTimer = Application.AddTimeout (TimeSpan.FromMilliseconds (200), TimerFn); + } + + void AppUnloaded (object? sender, EventArgs args) + { + if (_progressTimer is { }) + { + Application.RemoveTimeout (_progressTimer); + _progressTimer = null; + } } - void AppUnloaded (object sender, EventArgs args) { pulsing = false; } } } \ No newline at end of file