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; }