[VSX] How to be notified of the debug events ?

July 18th, 2014 | Posted by Tom in .NET | Article | Visual Studio | VSX | Read: 1,474

For an addin I’m workin on, I wanted to present a window to the user but only when Visual Studio is starting a debug session.

For that, I’ve found you can use the IVsDebuggerEvents interface which provides notifications when debugger changes mode.

On the class that need to be notified of the changes, get the current debugger and register for debug events:

uint _debugEventsCookie = VSConstants.VSCOOKIE_NIL;

_debugger = (IVsDebugger)this.GetService(typeof(IVsDebugger));
ErrorHandler.ThrowOnFailure(_debugger.AdviseDebuggerEvents(this, out _debugEventsCookie));

Once this is done, the method IVsDebuggerEvents.OnModeChange will be called so you’ll be able to know when the debugger changes mode:

int IVsDebuggerEvents.OnModeChange(DBGMODE dbgmodeNew)
    switch (dbgmodeNew)
        case DBGMODE.DBGMODE_Run:
            // Start Debug Session
        case DBGMODE.DBGMODE_Break:
            // Break Debug Session
        case DBGMODE.DBGMODE_Design:
            // Stop Debug Session

    return VSConstants.S_OK;

Of course, don’t forget to perform some cleanup when you’ve finished:

protected override void Dispose(bool disposing)
    if (_debugEventsCookie != VSConstants.VSCOOKIE_NIL && _debugger != null)
        ErrorHandler.CallWithCOMConvention(() => _debugger.UnadviseDebuggerEvents(_debugEventsCookie));

        _debugEventsCookie = VSConstants.VSCOOKIE_NIL;


That’s all (so simple isn’t it ? ;-)


Happy coding!

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Add Comment Register

Leave a Reply