Refactor debugger detection to use shared DebuggerHelper

Created a new DebuggerHelper class with Lazy<bool> 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<bool>
This commit is contained in:
Pierre Arnaud
2026-01-31 09:38:26 +01:00
parent a88e10bbf2
commit 0ee2bbe31c
3 changed files with 20 additions and 20 deletions

View File

@@ -0,0 +1,18 @@
namespace ElectronNET.Runtime.Helpers
{
using System;
using System.Diagnostics;
/// <summary>
/// Helper class for debugger detection with lazy initialization.
/// </summary>
internal static class DebuggerHelper
{
/// <summary>
/// Gets whether a debugger is attached. This value is cached for performance.
/// </summary>
public static bool IsAttached => _isAttached.Value;
private static readonly Lazy<bool> _isAttached = new Lazy<bool>(() => Debugger.IsAttached);
}
}

View File

@@ -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<Func<bool?>>();
@@ -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;
}

View File

@@ -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<Func<bool?>>();
@@ -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;
}