I created a VSIX Project Package and added some events, which should be called if the debugger is started.
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About
[ProvideAutoLoad(UIContextGuids80.Debugging)]
[Guid(FirstPackage.PackageGuidString)]
public sealed class FirstPackage : AsyncPackage, _dispDebuggerEvents
{
public const string PackageGuidString = "5ed493bb-1bfd-4946-a427-e400ee8653d9";
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
// Garbage Collector ?
await base.InitializeAsync(cancellationToken, progress);
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(DTE));
DebuggerEvents _debuggerEvents = dte.Events.DebuggerEvents;
_debuggerEvents.OnEnterRunMode += OnEnterRunMode;
_debuggerEvents.OnEnterBreakMode += OnEnterBreakMode;
}
public void OnEnterRunMode(dbgEventReason Reason)
{
// do some stuff
}
public void OnEnterBreakMode(dbgEventReason Reason, ref dbgExecutionAction ExecutionAction)
{
// do some other stuff
}
This is my current code. If I start the program, the events are initialized and by starting the debugger in my experitmental instance also triggered. My current problem is, it's working only the first time I debug the experimental instance.
You just need to ensure the DebuggerEvents doesn't go out of scope, so that your event handlers aren't eaten by the .NET Garbage Collector.
For example:
public sealed class FirstPackage : AsyncPackage, _dispDebuggerEvents
{
public const string PackageGuidString = "5ed493bb-1bfd-4946-a427-e400ee8653d9";
private DebuggerEvents _debuggerEvents;
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
// Garbage Collector ?
await base.InitializeAsync(cancellationToken, progress);
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(DTE));
_debuggerEvents = dte.Events.DebuggerEvents;
_debuggerEvents.OnEnterRunMode += OnEnterRunMode;
_debuggerEvents.OnEnterBreakMode += OnEnterBreakMode;
}