0

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.

Monie
  • 101
  • 8
  • Does this answer your question? [How do I subscribe to solution and project events from a VSPackage](https://stackoverflow.com/questions/7825489/how-do-i-subscribe-to-solution-and-project-events-from-a-vspackage) – Twenty Mar 08 '20 at 23:34

1 Answers1

1

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;
    }
Ed Dore
  • 2,013
  • 1
  • 10
  • 8
  • Already thought about the Garbage collector, but wasn't sure how to handle it. It's working now. Thank you! – Monie Mar 09 '20 at 05:45