Skip to content

Commit 6225605

Browse files
Added IsDebuggingAsync() and GetDebugModeAsync() methods
1 parent cc32409 commit 6225605

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.VisualStudio;
4+
using Microsoft.VisualStudio.Shell;
5+
using Microsoft.VisualStudio.Shell.Interop;
6+
7+
namespace Community.VisualStudio.Toolkit
8+
{
9+
/// <summary>
10+
/// Handles debugging.
11+
/// </summary>
12+
public class Debugger
13+
{
14+
/// <summary>
15+
/// The mode of the debugger.
16+
/// </summary>
17+
public enum DebugMode
18+
{
19+
/// <summary>The debugger is not attached.</summary>
20+
NotDebugging,
21+
/// <summary>The debugger is stopped at a breakpoint.</summary>
22+
AtBreakpoint,
23+
/// <summary>The debugger is attached and running.</summary>
24+
Running
25+
}
26+
27+
/// <summary>
28+
/// Checks if the debugger is attached.
29+
/// </summary>
30+
public async Task<bool> IsDebuggingAsync()
31+
{
32+
DebugMode debugMode = await GetDebugModeAsync();
33+
return debugMode != DebugMode.NotDebugging;
34+
}
35+
36+
/// <summary>
37+
/// Returns the current mode for the debugger.
38+
/// </summary>
39+
public async Task<DebugMode> GetDebugModeAsync()
40+
{
41+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
42+
43+
IVsDebugger debugger = await VS.Services.GetDebuggerAsync();
44+
DBGMODE[] mode = new DBGMODE[1];
45+
ErrorHandler.ThrowOnFailure(debugger.GetMode(mode));
46+
DBGMODE dbgMode = mode[0] & ~DBGMODE.DBGMODE_EncMask;
47+
48+
return dbgMode switch
49+
{
50+
DBGMODE.DBGMODE_Design => DebugMode.NotDebugging,
51+
DBGMODE.DBGMODE_Break => DebugMode.AtBreakpoint,
52+
DBGMODE.DBGMODE_Run => DebugMode.Running,
53+
_ => throw new InvalidOperationException($"Unexpected {nameof(DebugMode)}")
54+
};
55+
}
56+
}
57+
}

src/toolkit/Community.VisualStudio.Toolkit.Shared/VS.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public static class VS
2121
/// <summary>A collection of services related to the command system.</summary>
2222
public static Commands Commands => _commands ??= new();
2323

24+
private static Debugger? _debugger;
25+
/// <summary>A collection of services related to the debugger.</summary>
26+
public static Debugger Debugger => _debugger ??= new();
27+
2428
private static Documents? _documents;
2529
/// <summary>Contains helper methods for dealing with documents.</summary>
2630
public static Documents Documents => _documents ??= new();

src/toolkit/Community.VisualStudio.Toolkit.Shared/VSSDK.Helpers.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<Import_RootNamespace>Community.VisualStudio.Toolkit.Shared</Import_RootNamespace>
1010
</PropertyGroup>
1111
<ItemGroup>
12+
<Compile Include="$(MSBuildThisFileDirectory)Debugger\Debugger.cs" />
1213
<Compile Include="$(MSBuildThisFileDirectory)\Attributes\ProvideBraceCompletionAttribute.cs" />
1314
<Compile Include="$(MSBuildThisFileDirectory)\Attributes\ProvideFileIconAttribute.cs" />
1415
<Compile Include="$(MSBuildThisFileDirectory)\Attributes\ProvideGalleryFeedAttribute.cs" />

0 commit comments

Comments
 (0)