I am currently experimenting with Visual Studio Extensions. I need to subscribe to an event that gets called before the actual Debugger is stopped. Basically I am just Attaching to Managed Process (not running via F5). The problem is that Stop Debugging simply "detaches" the process, and the process continues running after that. I plan to use this event to notify our process to exit
I have a class that implements IDebugEventCallback2, IVsDebuggerEvents and IVsDebugProcessNotify.
class MyDebugger : IDebugEventCallback2, IVsDebuggerEvents, IVsDebugProcessNotify
Inside this class, there is a member that subscribes to Debugger Events using IVsDebugger's AdviseDebugEventCallback() and AdviseDebuggerEvents() events.
_debugger = Package.GetGlobalService(typeof(SVsShellDebugger)) as IVsDebugger;
if (_debugger != null)
{
_debugger.AdviseDebugEventCallback(this);
_debugger.AdviseDebuggerEvents(this, out _debuggerEventsCookie);
}
I noticed however that the events fired from AdviseDebugEventCallback's Event() handler does not always gets called before the actual Stop Debugging (next few lines after the breakpoint are still executed after I clicked Stopped Debugging). Around 4 or out of 5 times, the event from IDebugCustomEvent110 (riidEvent of 2615D9BC-1948-4D21-81EE-7A963F20CF59) gets called before any line from the attached process gets further executed. I still have to digest the details of the events fired in the Event() handler, but looking at the breakpoints, seems like I could not rely on this as it only works as per my expectation around 4 out of 5 times.
I am currently looking at the BeforeStopDebuggingProcess() method inside IVsDebugProcessNotify.
However, I don't know how to "Subscribe" or "Advice" from this interface.
Any advice how? There isn't much Google result about this topic.
Thank you!
I found something hacky, please comment if this is recommended or not. I got some hint about the
CommandEventsfrom this post: How do I know from my VSIX that a build will be followed by a Debug session?First, I subscribed to the commandEvents of DTE.
Then, inside
OnBeforeExecute, I am hardcoding this particular GUID and ID which I observed to be fired whenever Stop Debugging is clicked (amongst many other events). If I putThread.Sleep()of 30 seconds inside this handler, the Stop Button of the Visual Studio freezes for 30 seconds (eventually the entire Visual Studio) :-) The code will resume after 30 seconds of wait.