locked
How to hook into Debug.Start command (F5 event)? RRS feed

  • Question

  • Hello,

    I would like to get a callback when F5 is pressed or Debug.Start command is executed (equivalently).

    Its important that it happens only on F5 and not on "Attach to Process".

    On that callback I want to execute some code (my function).

    Any idea how to accomplish that?

    Thanks,

    Ofir

    Monday, February 6, 2017 10:35 PM

Answers

  • Sergey,

    Thank you so much for your help.

    The SO link was helpful, but I have two questions:

    1. What is the overhead incurred by intercepting *all* events?
    2. I tried to hook only the "Debug.Start" command, but the results were inconsistent
      I used the following code:

                EnvDTE.DTE dte = Package.GetGlobalService(typeof(SDTE)) as EnvDTE.DTE;
                EnvDTE.Command debugStartCmd = dteService.Commands.Item("Debug.Start");
                dte.Events.CommandEvents[debugStartCmd.Guid, debugStartCmd.ID].BeforeExecute += OnBeforeExecute;
                dte.Events.CommandEvents[debugStartCmd.Guid, debugStartCmd.ID].AfterExecute += OnAfterExecute;

    What I've observed is that sometimes the "OnBeforeExecute" is called, some other times the "OnAfterExecute" is called, and sometimes none of them are called.

    However, when I subscribe to all events:

    commandEvents = events.get_CommandEvents(null, 0);

    commandEvents.BeforeExecute += OnBeforeExecute;

    commandEvents.AfterExecute += OnAfterExecute;

    It always works (the callback/event handler always gets called for F5, Start Debugging command).

    Any idea?

    Thanks!

    - Ofir

    • Marked as answer by Ofir Cohen Wednesday, February 8, 2017 2:28 PM
    Tuesday, February 7, 2017 1:02 PM

All replies

  • Sergey,

    Thank you so much for your help.

    The SO link was helpful, but I have two questions:

    1. What is the overhead incurred by intercepting *all* events?
    2. I tried to hook only the "Debug.Start" command, but the results were inconsistent
      I used the following code:

                EnvDTE.DTE dte = Package.GetGlobalService(typeof(SDTE)) as EnvDTE.DTE;
                EnvDTE.Command debugStartCmd = dteService.Commands.Item("Debug.Start");
                dte.Events.CommandEvents[debugStartCmd.Guid, debugStartCmd.ID].BeforeExecute += OnBeforeExecute;
                dte.Events.CommandEvents[debugStartCmd.Guid, debugStartCmd.ID].AfterExecute += OnAfterExecute;

    What I've observed is that sometimes the "OnBeforeExecute" is called, some other times the "OnAfterExecute" is called, and sometimes none of them are called.

    However, when I subscribe to all events:

    commandEvents = events.get_CommandEvents(null, 0);

    commandEvents.BeforeExecute += OnBeforeExecute;

    commandEvents.AfterExecute += OnAfterExecute;

    It always works (the callback/event handler always gets called for F5, Start Debugging command).

    Any idea?

    Thanks!

    - Ofir

    • Marked as answer by Ofir Cohen Wednesday, February 8, 2017 2:28 PM
    Tuesday, February 7, 2017 1:02 PM
  • Ofir,

    I don't think overhead incurred by intercepting all events is noticeable, but of course intercepting only "Debug.Start" is better.

    You can try to add explicit references to events and other command objects to prevent them from garbage collection by introducing variables like events = dte.Events etc.


    Sergey Vlasov | Vlasov Studio | Visual Studio add-ins, extensions and tools

    Tuesday, February 7, 2017 6:29 PM
  • Sergey,

    I tried it but it just didn't work, you need to globally subscribe to all events which is fine

    for me for now as long as I've achieved my goal.


    Thanks!

    - Ofir

    Wednesday, February 8, 2017 2:30 PM