Visual Studio 扩展在停止调试进程之前捕获事件 (IVsDebugProcessNotify BeforeStopDebuggingProcess) [英] Visual Studio Extension capturing event before stopping the Debug Process ( IVsDebugProcessNotify BeforeStopDebuggingProcess)

查看:20
本文介绍了Visual Studio 扩展在停止调试进程之前捕获事件 (IVsDebugProcessNotify BeforeStopDebuggingProcess)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在试验 Visual Studio 扩展.我需要订阅一个在实际调试器停止之前被调用的事件.基本上我只是附加到托管进程(不通过 F5 运行).问题是停止调试只是简单地分离"进程,然后进程继续运行.我打算用这个事件通知我们的进程退出

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

我有一个实现 IDebugEventCallback2IVsDebuggerEventsIVsDebugProcessNotify 的类.

I have a class that implements IDebugEventCallback2, IVsDebuggerEvents and IVsDebugProcessNotify.

class MyDebugger : IDebugEventCallback2, IVsDebuggerEvents, IVsDebugProcessNotify

在这个类中,有一个成员使用 IVsDebuggerAdviseDebugEventCallback()AdviseDebuggerEvents() 事件订阅调试器事件.

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);
}

然而,我注意到从 AdviseDebugEventCallback 的 Event() 处理程序触发的事件并不总是在实际停止调试之前被调用(断点后的下几行在我单击停止调试后仍会执行)).大约 4 次或 5 次中,来自 IDebugCustomEvent110 的事件(2615D9BC-1948-4D21-81EE-7A963F20CF59 的 riidEvent)在来自附加进程的任何行被进一步执行之前被调用.我仍然需要消化 Event() 处理程序中触发的事件的详细信息,但查看断点,似乎我不能依赖它,因为它只能按照我的预期工作,大约 5 次中有 4 次.

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.

我目前正在查看 IVsDebugProcessNotify 中的 BeforeStopDebuggingProcess() 方法.但是,我不知道如何订阅"或建议"从这个界面.任何建议如何?关于这个话题的谷歌搜索结果并不多.谢谢!

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!

推荐答案

我发现了一些问题,如果推荐与否,请发表评论.我从这篇文章中得到了一些关于 CommandEvents 的提示:如何从我的 VSIX 中知道构建之后将进行调试会话?

I found something hacky, please comment if this is recommended or not. I got some hint about the CommandEvents from this post: How do I know from my VSIX that a build will be followed by a Debug session?

首先,我订阅了 DTE 的 commandEvents.

First, I subscribed to the commandEvents of DTE.

DTE dte = await serviceProvider.GetServiceAsync(typeof(DTE)) as DTE;
if (dte != null)
{
    events = dte.Events;
    commandEvents = events.CommandEvents;
    commandEvents.BeforeExecute += OnBeforeExecute;
}

然后,在 OnBeforeExecute 中,我对这个特定的 GUID 和 ID 进行了硬编码,我观察到每当单击停止调试"(以及许多其他事件)时都会触发它们.如果我将 Thread.Sleep() 的 30 秒放在此处理程序中,Visual Studio 的停止按钮会冻结 30 秒(最终整个 Visual Studio):-) 代码将在 30 秒后恢复等等.

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 put Thread.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.

private void OnBeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
    if (Guid == "{5EFC7975-14BC-11CF-9B2B-00AA00573819}" && ID == 179)
    {
        // Stop command is detected
        Trace.WriteLine($"[OnBeforeExecute] {Guid} --- {ID} Stop Command Detected");
        System.Threading.Thread.Sleep(30000);
    }
}

这篇关于Visual Studio 扩展在停止调试进程之前捕获事件 (IVsDebugProcessNotify BeforeStopDebuggingProcess)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆