首页 > 解决方案 > 如何在 EnvDTE.Events.DebuggerEvents 的处理程序中确定已调试的程序/进程?

问题描述

我正在创建一个 Visual Studio 扩展,它在特定应用程序的调试停止时执行一些任务。这是我处理调试器事件的代码:

    ...
    DTE2 ide = Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService(typeof(DTE)) as DTE2;
    if (ide != null)
    {
        debuggerEvents = ide.Events.DebuggerEvents;
        debuggerEvents.OnEnterDesignMode += DebuggerEvents_OnEnterDesignMode;
    }
}

private static void DebuggerEvents_OnEnterDesignMode(dbgEventReason Reason)
{
    ThreadHelper.ThrowIfNotOnUIThread();

    DTE2 ide = Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService(typeof(DTE)) as DTE2;
    if (ide != null && ide.Debugger.CurrentProcess != null)
    {
        DebuggedProcName = ide.Debugger.CurrentProcess.Name;
    }

    if (Reason == dbgEventReason.dbgEventReasonStopDebugging &&
        DebuggedProcName == "MyApp")
    {
        ...
    }
}

问题是 ide.Debugger.CurrentProcess 和 .CurrentProgram 在 OnEnterDesignMode() 中为空。它们在 OnEnterBreakMode() 中不为空,但可能不会被调用。如何确定 Visual Studio 扩展中当前调试的程序/进程?

标签: visual-studio-2019visual-studio-extensions

解决方案


如果某个项目的调试停止,我想执行一项特定任务。由于使用事件处理程序似乎不可能,我帮助自己使用了一个在按 Ctrl + F5 时执行的菜单命令。执行任务后,此命令还会终止已调试的进程,从而有效地停止调试。我想这很粗鲁,但我可以接受这个解决方案。


推荐阅读