From 0ee2bbe31c480bbbf0023875351a29d1bae5e20d Mon Sep 17 00:00:00 2001 From: Pierre Arnaud Date: Sat, 31 Jan 2026 09:38:26 +0100 Subject: [PATCH] Refactor debugger detection to use shared DebuggerHelper Created a new DebuggerHelper class with Lazy initialization that is shared by both LaunchOrderDetector and UnpackagedDetector. This eliminates code duplication and provides a cleaner, more maintainable solution. - Added DebuggerHelper.cs with lazy-initialized IsAttached property - Refactored LaunchOrderDetector to use DebuggerHelper - Refactored UnpackagedDetector to use DebuggerHelper - Removed nullable bool pattern in favor of Lazy --- .../Runtime/Helpers/DebuggerHelper.cs | 18 ++++++++++++++++++ .../Runtime/Helpers/LaunchOrderDetector.cs | 11 +---------- .../Runtime/Helpers/UnpackagedDetector.cs | 11 +---------- 3 files changed, 20 insertions(+), 20 deletions(-) create mode 100644 src/ElectronNET.API/Runtime/Helpers/DebuggerHelper.cs diff --git a/src/ElectronNET.API/Runtime/Helpers/DebuggerHelper.cs b/src/ElectronNET.API/Runtime/Helpers/DebuggerHelper.cs new file mode 100644 index 0000000..237775a --- /dev/null +++ b/src/ElectronNET.API/Runtime/Helpers/DebuggerHelper.cs @@ -0,0 +1,18 @@ +namespace ElectronNET.Runtime.Helpers +{ + using System; + using System.Diagnostics; + + /// + /// Helper class for debugger detection with lazy initialization. + /// + internal static class DebuggerHelper + { + /// + /// Gets whether a debugger is attached. This value is cached for performance. + /// + public static bool IsAttached => _isAttached.Value; + + private static readonly Lazy _isAttached = new Lazy(() => Debugger.IsAttached); + } +} diff --git a/src/ElectronNET.API/Runtime/Helpers/LaunchOrderDetector.cs b/src/ElectronNET.API/Runtime/Helpers/LaunchOrderDetector.cs index 345af9f..f789eed 100644 --- a/src/ElectronNET.API/Runtime/Helpers/LaunchOrderDetector.cs +++ b/src/ElectronNET.API/Runtime/Helpers/LaunchOrderDetector.cs @@ -7,9 +7,6 @@ internal class LaunchOrderDetector { - // Cache debugger state to avoid multiple expensive checks - private static bool? _debuggerAttached; - public static bool CheckIsLaunchedByDotNet() { var tests = new List>(); @@ -64,13 +61,7 @@ private static bool? CheckIsDotNetStartup3() { - // Cache debugger state to avoid multiple expensive checks - if (_debuggerAttached == null) - { - _debuggerAttached = Debugger.IsAttached; - } - - if (_debuggerAttached.Value) + if (DebuggerHelper.IsAttached) { return true; } diff --git a/src/ElectronNET.API/Runtime/Helpers/UnpackagedDetector.cs b/src/ElectronNET.API/Runtime/Helpers/UnpackagedDetector.cs index ab7e21b..a6fc3f1 100644 --- a/src/ElectronNET.API/Runtime/Helpers/UnpackagedDetector.cs +++ b/src/ElectronNET.API/Runtime/Helpers/UnpackagedDetector.cs @@ -8,9 +8,6 @@ internal class UnpackagedDetector { - // Cache debugger state to avoid multiple expensive checks - private static bool? _debuggerAttached; - public static bool CheckIsUnpackaged() { var tests = new List>(); @@ -85,13 +82,7 @@ private static bool? CheckUnpackaged3() { - // Cache debugger state to avoid multiple expensive checks - if (_debuggerAttached == null) - { - _debuggerAttached = Debugger.IsAttached; - } - - if (_debuggerAttached.Value) + if (DebuggerHelper.IsAttached) { return true; }